gpt4 book ai didi

php - 将 JSON 解码保存到 MySQL、PHP

转载 作者:行者123 更新时间:2023-11-29 20:12:21 25 4
gpt4 key购买 nike

美好的一天,请你帮忙,我正在尝试使用 JSON POST 更新 mysql 表。 JSON 输出到文本文件中很好,但是当我尝试将其保存到 MySQL 表时,会出现错误:

Undefined index: 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 或类似的准备好的/参数化查询。

关于php - 将 JSON 解码保存到 MySQL、PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39999070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com