使用JSON无法正常插入MySQL

时间:2015-08-06 15:28:29

标签: php python mysql json

我在以下代码中有什么错误:

<?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 = '{"device":"gabriel","data_type":"data","zone":1,"sample":4,"count":0,"time_stamp":"00:00"}'

$payload_array = json.decode($payload,true);

//get the data_payload details
$device = $payload_array['device'];
$type = $payload_array['data_type'];
$zone = $payload_array['zone'];
$sample = $payload_array['sample'];
$count = $payload_array['count'];
$time = $payload_array['time_stamp'];

$sql = "INSERT INTO data(device, data_type, zone, sample, count, time_stamp) VALUES('$device', '$type', '$zone', '$sample', '$count', '$time')";

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


$conn->close();
?>

我收到以下错误:

解析错误:语法错误,第18行/Applications/XAMPP/xamppfiles/htdocs/insert_from_json.php中的意外'$ payload_array'(T_VARIABLE)

此PHP文件通过JSON从python文件接收“payload”。 PHP文件将该数据插入表中。 $ payload是一个测试变量,用于“模拟”来自上面一行中的python代码的数据。有效负载可以包含多行。也许有更好的方法来插入多行而不是我正在尝试的行?

1 个答案:

答案 0 :(得分:1)

在第18行,您有以下内容:

$payload_array = json_decode($payload,true);

什么时候应该

var f=$('.col-sm-10 ul li').attr('data-value');
var productId = f.match(/product_id=([^&]+)/)[1];

您使用句点(。)而不是下划线(_)。