<div data-id=1448 class='item folder'>lorem ipsum</div>
<div data-id=1393 class='item folder'>lorem ipsum</div>
<div data-id=1441 class='item folder'>lorem ipsum</div>
<div data-id=1442 class='item folder'>lorem ipsum</div>
js
$('#mpsave').click(function(){
var ids = [];
var indexes = [];
$('.folder').each(function(){
ids.push($(this).data('id'));
indexes.push($(this).index());
});
console.log(ids); // works well
console.log(indexes); // works well
$.ajax({
url: 'target.php',
type: 'post',
data: {'ids' : ids, 'indexes' : indexes},
success: function(data){
console.log(data); // works well
}
});
});
target.php
extract($_POST);
print_r($ids);
print_r($indexes);
因此,数组在php端。现在,我需要以这种方式更新数据库:
- 对于每个ids
元素,找到相应的行
- 使用并行inde
元素的值更新行(indexes
列)。
这样的事情:
try {
$stmt = $db->prepare("UPDATE folders SET inde = :inde WHERE id = :id");
$stmt->execute(array(
":id" => $ids-element,
":inde" => $indexes-element
));
}
catch(PDOException $e) {
echo $e->getMessage();
}
任何帮助?
答案 0 :(得分:1)
你甚至可以在php变量名中使用连字符吗?尝试:
for ($i = 0; $i <= count($ids); $i++){
try {
$stmt = $db->prepare("UPDATE folders SET inde = ? WHERE id = ?");
$stmt->bind_param("ii",$ids[i],$indexes[i]);
$stmt->execute();
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
http://php.net/manual/en/mysqli.quickstart.prepared-statements.php http://php.net/manual/en/mysqli-stmt.bind-param.php