开发者

How to decode JSON in PHP and insert it into Mysql table?

开发者 https://www.devze.com 2023-03-26 22:05 出处:网络
I\'m using the following code in PHP. Its creating the file post.json, but inserting values into the table is not reflecting.

I'm using the following code in PHP.

Its creating the file post.json, but inserting values into the table is not reflecting.

<?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,开发者_JS百科$str."\n");
    fclose($fd);
}

?>


Try to replace '$json_a[foo]' with '" . $json_a['foo'] . "' or '${json_a['foo']}'.

0

精彩评论

暂无评论...
验证码 换一张
取 消