簡體   English   中英

如何在PHP中解碼JSON並將其插入Mysql表?

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

我在PHP中使用以下代碼。

它創建了文件post.json ,但是沒有在表中插入值。

<?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']}'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM