2016-03-23 60 views
1

我正在使用jQuery和ajax有一个拖放式可排序表。Php查询在循环中不更新数据库

我的问题是,它只能工作大约10%的时间。

这是我的js代码。

<script type="text/javascript"> 
// When the document is ready set up our sortable with it's inherant function(s) 
$(document).ready(function() { 
    $("#test-list").sortable({ 
     handle : '.handle', 
     update : function() { 
      var order = $('#test-list').sortable('serialize'); 
      $("#info").load("tow_sort2.php?"+order); 
     } 
    }); 
}); 
</script> 

这里是tow_sort2.php页:

<?php 
include("../_includes/db_connection.php"); 

/** 
* This is where you would inject your sql into the database 
* but we're just going to format it and send it back 
*/ 
$array =array(); 
foreach ($_GET['listItem'] as $position => $item) 
{ 
    $sql = "UPDATE tow SET tow_order = '$position' WHERE tow_id = '$item'"; 
    $conn->query($sql); 
    $array[] = $sql; 
} 
print_r($array); 
?> 

这里是正在打印数组:

Array ([0] => UPDATE tow SET tow_order = '0' WHERE tow_id = '5924' [1] => UPDATE tow SET tow_order = '1' WHERE tow_id = '5925' [2] => UPDATE tow SET tow_order = '2' WHERE tow_id = '5922' [3] => UPDATE tow SET tow_order = '3' WHERE tow_id = '5923') 

Screenshot of the Table

+0

当它不起作用时,您是否收到任何错误消息? –

+0

什么是数据库提供者?如何管理交易? – soyuka

+0

预期行为是什么?它究竟如何不工作? – BPS

回答

0

我认为这个问题是你正在使用方法查询而不是exec:

$conn->exec($sql); 

顺便说一下,你的代码是可以sql注入的。请改用prepared声明或检查变量。