簡體   English   中英

將JSON解碼保存到MySQL,PHP

[英]Save JSON Decode to MySQL , PHP

美好的一天,您能協助我嘗試使用JSON POST更新mysql表嗎? 將JSON輸出輸出到文本文件中很好,但是當我嘗試將其保存到MySQL表中時出現錯誤:

未定義索引:ptp.create

JSON將數據輸出為:

{"ptp.create":["629","630"]}

$jsonString = file_get_contents("php://input");
$myFile = "NewFile.txt";
file_put_contents($myFile,$jsonString);

$data = json_decode($jsonString, true);

foreach ($data as $row){
$PTP = $row['ptp.create'];
$query_rsTable1 = "INSERT INTO test SET id = '$PTP'";
$rsTable1 = mysql_query($query_rsTable1, $int) or die(mysql_error());
}

我對JSON並不是100%的自信,如果可以的話,請提供幫助。

您的for循環已經為您處理了ptp.create

foreach ($data as $ptp){
  echo $ptp;  // Will be 629, then 630
}

當您實際插入數據庫時​​,請使用具有PDO或類似功能的准備好的/參數化的查詢。

暫無
暫無

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

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