简体   繁体   中英

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

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,$str."\n");
    fclose($fd);
}

?>

尝试将'$json_a[foo]'替换为'" . $json_a['foo'] . "''${json_a['foo']}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM