PHP에서 다음 코드를 사용하고 있습니다.PHP에서 JSON을 디코딩하고이를 MySQL 테이블에 삽입하는 방법은 무엇입니까?
파일을 생성하는 중 post.json
이지만 테이블에 값을 삽입하는 것은 반영되지 않습니다. '" . $json_a['foo'] . "'
또는 '${json_a['foo']}'
로 '$json_a[foo]'
을 대체 할
<?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);
}
?>
insert 문에서 $ json_a 배열은 '$ {json_a ['TableId ']}' ' –