【发布时间】:2015-07-24 08:43:27
【问题描述】:
我想在 mysql 表中插入 json 数组数据。我已经写了这段代码。
if (mysqli_connect_errno()){
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
if(isset($_GET['doctorJson'])){
$json = $_GET['doctorJson'];
$array = json_decode($json, true);
foreach($array as $item){
$result = mysqli_query($con, "INSERT IGNORE INTO doctor_visit_track (id, doctor_name, doctor_email, date, time) VALUES
('".$item['id']."', '".$item['doctorName']."', '".$item['doctorEmail']."', '".$item['date']."', '".$item['time']."')");
}
if($result){
$response["message"] = "Success";
echo json_encode($response);
} else{
$response["message"] = "Failure";
echo json_encode($response);
}
}
mysqli_close($con);
当我使用 xampp 时,上面的代码运行良好。但是,当我将此代码上传到服务器时,相同的代码会发出警告“foreach() 提供的参数无效”并且未插入表中。但是在 xampp 中使用,代码工作正常并成功插入数据。有人帮帮我..
【问题讨论】:
-
var_dump($array)看看你得到了什么。 -
@b0s3 我得到 NULL..
-
这意味着
$json无效JSON。检查一下。 -
如果有帮助,您可以通过
json_last_error_msg()获取有关json_decode()遇到的错误的更多信息 -
@b0s3 但是为什么使用 xampp 时相同的代码可以成功运行..