我在 PHP 中使用以下代码。
它创建了文件 post.json
,但没有反射(reflect)向表中插入值。
<?php
$input = file_get_contents('php://input');
logToFile("post.json",$input);
$json_a = json_decode($input,true);
$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);
mysql_query("INSERT INTO order_table (Table_id, Menu_Item_Name, Menu_Item_Price, Menu_Item_quantity) VALUES('$json_a[TableId]','$json_a[ItemName]','$json_a[ItemPrice]','$json_a[ItemQuantity]')");
function logToFile($filename,$msg)
{
$fd = fopen($filename,"a");
$str="[".date("Y/m/d h:i:s")."]".$msg;
fwrite($fd,$str."\n");
fclose($fd);
}
?>
最佳答案
尝试将 '$json_a[foo]'
替换为 '". $json_a['foo'] . "'
或 '${json_a[ 'foo']}'
.
关于php - 如何在 PHP 中解码 JSON 并将其插入到 Mysql 表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6982682/