【发布时间】:2020-08-09 10:38:25
【问题描述】:
我正在使用 json_decode() 来解码 Ajax 结果,该结果返回以下数组(缩短的示例)。 现在我想遍历这个区域并根据数组中的值更新每个 vId 的 rId 但这部分不起作用。
有人可以在这里告诉我如何正确执行循环部分(查询本身应该没问题)吗?
我的数组:
array(3) {
[0]=>
array(2) {
["vId"]=>
string(8) "04567901"
["rId"]=>
string(6) "DE-003"
}
[1]=>
array(2) {
["vId"]=>
string(8) "04567902"
["rId"]=>
string(6) "DE-008"
}
[2]=>
array(2) {
["vId"]=>
string(8) "04567903"
["rId"]=>
string(6) "DE-009"
}
}
我的 PHP/MySQLi:
$postData = $_POST;
$transferData = $_POST['transferData'];
$json = json_decode($transferData, true);
$conn = new mysqli($host, $username, $password, $database);
if($conn->connect_error) {
die("Connection Error: " . $conn->connect_error);
}
$stmt = $conn->prepare("UPDATE locations l SET l.rId = ? WHERE l.vId = ?");
foreach($json as $vId => $rId) {
$stmt->bind_param('ss', $rId, $vId);
$stmt->execute();
}
$stmt->close();
$conn->close();
【问题讨论】: