Php Query In循环不更新数据库

时间:2016-03-23 15:24:48

标签: php mysql ajax

我正在使用jquery和ajax来拖放可排序表。

我的问题是它只能在大约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

1 个答案:

答案 0 :(得分:0)

我认为问题在于您使用的是方法查询而不是exec:

$conn->exec($sql);

顺便说一下,你的代码对于sql注入是很容易的。请改用prepared语句或检查变量。