2016-03-23 91 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聲明或檢查變量。