我正在使用jquery可排序並試圖將這些項目的位置保存到名爲「位置」的mysql表的行中。我似乎無法讓他們保存/更新正確。一切工作正常jQuery的一面,只是PHP的更新功能。現在它將位置行保存爲3,這是我得到的原因。我似乎無法圍繞如何正確做到這一點。MYSQL更新根據一定的順序從jquery排序
下面是我對PHP:
$id = $_POST['id'];
$arr = $_POST['positions'];
foreach($arr as $r) {
$sql = " UPDATE items
SET position = '$r'
WHERE groupId = '$id' ";
}
表結構/所需的輸出:
id | groupId | position
----------------------------
3 10 0
6 8 -
8 10 3
10 5 -
15 10 2
18 10 1
jQuery的文件:
$("#items").sortable({
update: function() {
var invId = $("input[name='deleteId']").val();
var post = $(this).sortable('serialize');
$.ajax({
type: 'POST',
url: 'file.php',
data: { positions: post, id: invId },
dataType: 'JSON',
cache: false,
success: function(output) {
console.log('success');
},
error: function(output) {
console.log('fail ');
}
});
}
});
感謝。
那'$ arr'不是數組雖然 – Qirel