Php Query In循环不更新数据库

标签 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

最佳答案

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

$conn->exec($sql);

顺便说一句,你的代码很容易被sql注入(inject)攻击。请使用prepared语句或检查变量。

关于Php Query In循环不更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36182203/

相关文章:

php - 如何将 Blade 模板文件扩展名从 *.blade.php 更改为仅 *.blade

php - 检查 mysql 行是否存在时,foreach 中的嵌套 if/then 语句失败

php - 简单的 PHP 登录页面帮助 - PHP、MySql、HTML

mysql - 如何将空间数据插入包含道路列的表中

mysql - Mysql存储过程中的整数数组

javascript - 为什么我的 ajax 调用失败

Java通过套接字接收AJAX请求

javascript - Uncaught ReferenceError : error is not defined in Ajax Callback

php - AJAX/JQUERY 不更新 MySQL 行

php - 在php中将二进制字符串转换为整数