我的代码是:
//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}
你能帮我解决这个问题吗?
答案 0 :(得分:0)
尝试此代码,您不需要两个循环
<?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++;
}
?>
答案 1 :(得分:-1)
我已经编辑了你的代码。
<?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) {
$data = array();
$data['x'] = $entry->x;
$data['y'] = $entry->y;
$data['width'] = $entry->width;
$data['height'] = $entry->height;
$positionjson = json_encode($data);
//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);
}
}
?>