gpt4 book ai didi

php - 将解码后的数据存储到不同的 ID

转载 作者:行者123 更新时间:2023-11-30 23:47:17 24 4
gpt4 key购买 nike

我的代码是:

//This is the data I am getting [{"x":1,"y":0,"width":2,"height":10},{"x":6,"y":0,"width":2,"height":9}] 

<?php
$position = json_decode($_POST['positionData'], true);
$select_id = "SELECT id FROM homegrid";
$select_id_exec = mysql_query($select_id);
//print_r($position[1]);
//print_r($position[2]);
//getting result seperately
foreach ($position as $entry) {
$x = $entry['x'];
$y = $entry['y'];
$width = $entry['width'];
$height = $entry['height'];
$positionjson = json_encode($entry);
//print_r($positionjson);
while($idFromDB = mysql_fetch_assoc($select_id_exec)) {
//print_r($idFromDB);
//echo $update = "update homegrid set position = '$positionjson' WHERE id = '" . $idFromDB['id'] . "' ";
//mysql_query($update);
}
}
?>

更新查询的输出是

update homegrid set position = '{"x":6,"y":0,"width":2,"height":9}' WHERE id = '7' 
update homegrid set position = '{"x":6,"y":0,"width":2,"height":9}' WHERE id = '8'

表示获取数组最后位置的结果。

我怎样才能得到这样的结果

update homegrid set position = '{"x":1,"y":0,"width":2,"height":10}' WHERE id = '7' 
update homegrid set position = '{"x":6,"y":0,"width":2,"height":9}' WHERE id = '8'

?

我的表结构如下所示

id position
7 {"x":1,"y":0,"width":2,"height":10}
8 {"x":6,"y":0,"width":2,"height":9}

你能帮我解决这个问题吗?

最佳答案

试试这个代码你不需要两个循环

<?php
$position = json_decode($_POST['positionData'], true);
$select_id = "SELECT id FROM homegrid";
$select_id_exec = mysql_query($select_id);
//print_r($position[1]);
//print_r($position[2]);
//getting result seperately
$i = 0;
while($idFromDB = mysql_fetch_assoc($select_id_exec)) {
$x = $position[$i]['x'];
$y = $position[$i]['y'];
$width = $position[$i]['width'];
$height = $position[$i]['height'];
$positionjson = json_encode($position[$i]);
//print_r($idFromDB);
$update = "update homegrid set position = '$positionjson' WHERE id = '" . $idFromDB['id'] . "' ";
mysql_query($update);
$i++;
}
?>

关于php - 将解码后的数据存储到不同的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31606673/

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