duankuixi1930 2015-08-07 15:48
浏览 152
已采纳

从JSON数组向MySQL插入多个空行

The below PHP code receives a JSON array from a Python script in the format of:

{'payload': '[{"sample": 1, "count": 60, "sqlTimeStamp": "2015-08-03 15:14:05", "id": 1, "zone": 1}, {"sample": 2, "count": 60, "sqlTimeStamp": "2015-08-03 15:14:07", "id": 2, "zone": 1}, {"sample": 3, "count": 60, "sqlTimeStamp": "2015-08-03 15:14:09", "id": 3, "zone": 1}]'}

The PHP code is as follows:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "practice";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connection made...";

$payload_dump = $_POST['payload'];
//$payload_dump = '{"device":"gabriel","data_type":"data","zone":1,"sample":6,"count":0,"time_stamp":"00:00"}';
echo $payload_dump;

$payload_array = json_decode($payload_dump,true);

if(is_array($payload_array)){
foreach($payload_array as $row){
//get the data_payload details
$sample = $payload_array['sample'];
$count = $payload_array['count'];
$time = $payload_array['sqlTimeStamp'];
$id = $payload_array['id'];
$zone = $payload_array['zone'];
//$time = $payload_array['time_stamp'];

$sql = "INSERT INTO data(sample, count, sqlTimeStamp, id, zone) VALUES('$sample', '$count', '$time', '$id', '$zone')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
$conn->close();
?>

The PHP script creates the correct number of new rows in the MySQL database, but all the values are zero/null. What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • duanchi5078 2015-08-07 16:01
    关注

    Assuming the JSON is actually like this:

    [{"sample": 1, "count": 60, "sqlTimeStamp": "2015-08-03 15:14:05", "id": 1, "zone": 1}, 
        {"sample": 2, "count": 60, "sqlTimeStamp": "2015-08-03 15:14:07", "id": 2, "zone": 1}, 
        {"sample": 3, "count": 60, "sqlTimeStamp": "2015-08-03 15:14:09", "id": 3, "zone": 1}]
    

    It looks like the problem is that you are looping over $payload_array, but you are not referencing the $row variable for the current iteration of the loop. So instead of

    foreach($payload_array as $row) {
        $sample = $payload_array['sample'];
        $count = $payload_array['count'];
        $time = $payload_array['sqlTimeStamp'];
        $id = $payload_array['id'];
        $zone = $payload_array['zone'];
    

    It needs to be

    foreach($payload_array as $row) {
        $sample = $row['sample'];
        $count = $row['count'];
        $time = $row['sqlTimeStamp'];
        $id = $row['id'];
        $zone = $row['zone'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?