从JSON INSERT INTO动态列

时间:2018-04-13 09:06:45

标签: php sql json pdo

这是我将静态数据插入数据库表的方法:

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
    // set the PDO error mode to exception
    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );

    $campaign = (isset($_POST['campaign'])) ? $_POST['campaign'] : null;
    $salutation = $_POST['salutation'];
    $firstname = $_POST['first_name'];
    $lastname = $_POST['surname'];
    $address = $_POST['address'];
    $zipcode = $_POST['zipcode'];
    $location = $_POST['location'];
    $country = $_POST['country'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $date_of_birth = (isset($_POST['day']) && is_numeric($_POST['day']) && isset($_POST['month']) && $_POST['month'] != 'Monat' && isset($_POST['year']) && is_numeric($_POST['year'])) ? $_POST['day'] . '.' . $_POST['month'] . '.' . $_POST['year'] : '';
    $ip = $_SERVER['REMOTE_ADDR'];
    $created_on = time();

    $stmt = $conn->prepare ("INSERT INTO t_weltwoche_address_infos2018_test (salutation, firstname, lastname, address, loctation, country, phone, email, date_of_birth, remoteAddress, createdon) VALUES (:salutation, :firstname, :lastname, :address, :loctation, :country, :phone, :email, :date_of_birth, :remoteAddress, :createdon)");

    $stmt -> bindParam(':salutation', $salutation);
    $stmt -> bindParam(':firstname', $firstname);
    $stmt -> bindParam(':surname', $lastname);
    $stmt -> bindParam(':address', $address);
    $stmt -> bindParam(':loctation', $location);
    $stmt -> bindParam(':country', $country);
    $stmt -> bindParam(':phone', $phone);
    $stmt -> bindParam(':email', $email);
    $stmt -> bindParam(':date_of_birth', $date_of_birth);
    $stmt -> bindParam(':remoteAddress', $ip);
    $stmt -> bindParam(':createdon', $created_on);
    $stmt -> execute();
}
catch(PDOException $e) {
    $conn = null;
    echo $e;
}

这只是一个测试而且似乎有效。现在我想动态地做所有事情。

$json = json_decode(file_get_contents('/srv/www/micro/weltwochen.lasttry.db.json'));

使用$json我得到一个JSON对象。我对JSON文件的值不感兴趣,但我想将它的属性用于我的MySQL表的列。

所有JSON属性的调用与$_POST[]的字符串相同。

示例

如果JSON具有名为first_name的属性,则插入$_POST['first_name']的值。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

如果您使用传递true作为json_decode的可选$assoc参数...

$json = json_decode( file_get_contents('/srv/www/micro/weltwochen.lasttry.db.json'),
                     true );

然后

$json变量已包含您询问的格式的数据。

实施例。您可以访问$json['first_name']

当然,如果解析的JSON编码数据具有以下格式(键值对的单个对象),则上述内容有效:

{ 
    "first_name":    "Reza",
    "surname":       "Saadati",
    "address"....

}

如果情况并非如此,那么解码后的数组$json需要被修复""。如何做到这一点取决于源JSON的格式;你可以在答案中包含这个,以防万一。

答案 1 :(得分:0)

您需要为prepare()构建查询字符串。这是使用字符串操作的一种方法。如果您控制并信任JSON文件,则此方法是可行的,但如果JSON文件来自不安全的源,则此代码可能容易受到SQL injection动态密钥的攻击。在这种情况下,我非常谨慎。

$json = json_decode(file_get_contents('/srv/www/micro/weltwochen.lasttry.db.json'), true);

$keys = array_keys($json);
$column_names = implode(", ", $keys);
$place_markers = implode(", ", array_map(function($column) {return ':'.$column;}, $keys));
$query = "INSERT INTO t_weltwoche_address_infos2018_test (" . $column_names . ") VALUES (" . $place_markers . ")";

$stmt = $conn->prepare($query);
foreach($keys as $key) {
    $stmt->bindParam($key, $_POST[$key]);
}