使用此代码,我在MySQL列中仅获得带有变量$date
的局部正确值,因为PHP给了我时间,因此它必须正确。我应该怎么做才能将JSON帖子保存到PHP变量并将其正确发送到SQL列?
这是我的JSON调用:
{
"buyer":{
"id":142694,
"email":"test@example.com",
"quantity":1,
"sent_count":0,
"created_at":"2018-12-29T20:29:23+01:00"
},
"listing":{
"id":36,
"name":"GTA V STEAM KEY"
},
"database":{
"id":16141,
"available_codes_count":7
},
"payment":{
"id":null,
"code":null,
"amount":null,
"currency":null,
"done":false
},
"created_at":"2018-12-29T20:29:23+01:00"
}
<?php
$json = json_decode($_POST);
$email = var_dump($json->buyer->email);
$name = var_dump($json->listing->name);
$count = var_dump($json->buyer->quantity);
$code = var_dump($json->payment->code);
date_default_timezone_set('Europe/Warsaw');
$date = date("Y-m-d H:i:s", time());
$servername = "localhost";
$username = "test1";
$password = "test2";
$dbname = "test3";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO list (email, name, date, count, code)
VALUES ('".$email."', '".$name."', '".$date."', '".$count."', '".$code."')";
if ($conn->query($sql) === true) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
答案 0 :(得分:0)
感觉就像我们没有沟通。这是我想问的一个功能全面的最小示例。
<?php
$json = '{
"buyer":{
"id":142694,
"email":"test@example.com",
"quantity":1,
"sent_count":0,
"created_at":"2018-12-29T20:29:23+01:00"
},
"listing":{
"id":36,
"name":"GTA V STEAM KEY"
},
"database":{
"id":16141,
"available_codes_count":7
},
"payment":{
"id":null,
"code":null,
"amount":null,
"currency":null,
"done":false
},
"created_at":"2018-12-29T20:29:23+01:00"
}';
$j = json_decode($json);
$someDate = date('Y-m-d H:i:s', strtotime($j->buyer->created_at));
更新:
好的,我想现在更好地理解您的问题。
特定于评论并了解var_dump
的功能。请尝试并理解它!:
<?php
$a = ['hello World'];
$b = var_dump($a); //this sends output, not what you want to do for an 'assignment'.
$c = $a;
var_dump($a); //desired results
var_dump($b); //null
现在,根据您的具体情况...
<?php
$json = json_decode($_POST['myJSON']);
// Question #1 - does this return an object? This is where you could use var_dump($json); to check.
//If it does...
$email = $json->buyer->email;
$name = $json->listing->name;
$date = date('Y-m-d H:i:s', strtotime($j->buyer->created_at));
// etc...
否则,您需要更新问题并实际显示在$_POST
中收到的内容。
答案 1 :(得分:0)
您应该使用准备好的语句,而不是将变量注入SQL。
关于您的问题,如何获取JSON POST数据,请参见以下帖子:Receive JSON POST with PHP
这里或多或少是您的代码的样子。
<?php
$json = json_decode(file_get_contents('php://input'));
date_default_timezone_set('Europe/Warsaw');
// $date = date("Y-m-d H:i:s", time()); // <- not needed because MySQL has CURDATE()/NOW()
// if for any reason you would like to pass date from PHP you can uncomment the above line and add anoter parameter to SQL
$servername = "localhost";
$username = "test1";
$password = "test2";
$dbname = "test3";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($servername, $username, $password, $dbname);
$stmt = $conn->prepare('INSERT INTO list (email, name, `date`, count, code) VALUES (?,?,CURDATE(),?,?)');
$stmt->bind_param(
'ssss',
$json->buyer->email,
$json->listing->name,
$json->buyer->quantity,
$json->payment->code
);
$stmt->execute();
echo "New record created successfully";