我正在将一个对象列表上传到MySQL。 某些对象不包含相同数量的变量。例如
Objectt1 {
property1
property 2
}
Objectt2 {
property1
property2
property3
}
Objectt3 {
property1
}
我的问题是mysql中的Object3被赋予了property2和property3而不是NULL。该值来自Object2。如何使对象3的propery2和property3为null? php代码如下:(我想我明白为什么会这样做,因为变量已经在之前的循环运行中已经设置了。但是我不知道如何修复它。)
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if($_SERVER["REQUEST_METHOD"] == "POST") {
require 'connection.php';
uploadstart();
}
function uploadstart() {
global $connect;
$json = $_POST["objectlist"];
//Remove Slashes
if (get_magic_quotes_gpc()){
$json = stripslashes($json);
}
$createworkoutquery = $connect->prepare("INSERT INTO objectTable
(id, prop1, prop2, prop3)
VALUES (?, ?, ?, ?)");
$createworkoutquery->bind_param("ssss", $ID, $prop1, $prop2, $prop3) ;
//convert json object to php associative array
$data = json_decode($json, true);
//Util arrays to create response JSON
$a=array();
$b=array();
// loop through the array
foreach ($data as $row) {
// get the list details
$ID = $row["id"];
$prop1 = $row["prop1"];
if (isset($row["prpop2"])) {
$prop2 = $row["prop2"];
}
if (isset($row["prop3"])) {
$prop3 = $row["prop3"];
}
// execute insert query
$result = $createworkoutquery->execute();
//if insert successful.. plug yes and woid else plug no
if($result){
$b["id"] = $ID;
$b["status"] = 'yes';
array_push($a,$b);
} else {
$b["id"] = $ID;
$b["status"] = 'no';
array_push($a,$b);
}
}
echo json_encode($a);
//close connection
mysqli_close($connect);
}
?>
答案 0 :(得分:2)
在每次迭代时将onBuildExit
分配给缺少的属性,以便之前的值不会被绑定到查询中:
null
答案 1 :(得分:0)
我注意到的一件事是错误的prpop2
conda create -n clusterenv python=3.4 scikit-learn=0.15 matplotlib pandas jupyter
activate clusterenv