这是我的PHP代码:MySQL的更新设置的所有字段相同
if (isset($_POST['data']) && is_array($_POST['data'])) {
foreach ($_POST['data'] as $row => $data) {
$result = mysql_query("UPDATE orders SET project_ref='".$data['project_ref']."' where order_id = '".$data['order_id']."'") or die(mysql_error());
$result1 = mysql_query("UPDATE orders SET supp_short_code='".$data['supp_short_code']."' where order_id = '".$data['order_id']."'") or die(mysql_error());
$result2 = mysql_query("UPDATE orders SET om_part_no='".$data['om_part_no']."' where order_id = '".$data['order_id']."'") or die(mysql_error());
$result3 = mysql_query("UPDATE orders SET description='".$data['description']."' where order_id = '".$data['order_id']."'") or die(mysql_error());
$result4 = mysql_query("UPDATE orders SET quantity='".$data['quantity_input']."' where order_id = '".$data['order_id']."'") or die(mysql_error());
$result5 = mysql_query("UPDATE orders SET cost_of_items='".$data['cost_of_items']."' where order_id = '".$data['order_id']."'") or die(mysql_error());
$result6 = mysql_query("UPDATE orders SET cost_total='".$data['cost_total_td']."' where order_id = '".$data['order_id']."'") or die(mysql_error());
}
}
因此,当用户希望编辑订单ID:1,我希望这将更新所有行的order_id为1,但什么这段代码正在将所有字段设置为“1”?
编辑:
这是如何我发送数据到PHP:
$('#submit').live('click',function(){
var postData = {};
postData['data[order_id]'] = $('#order_id').text();
$('#items tr').not(':first').each(function(index, value) {
var keyPrefix = 'data[' + index + ']';
postData[keyPrefix + '[supp_short_code]'] = $(this).closest('tr').find('.supp_short_code').text();
postData[keyPrefix + '[project_ref]'] = $(this).closest('tr').find('.project_ref').text();
postData[keyPrefix + '[om_part_no]'] = $(this).closest('tr').find('.om_part_no').text();
postData[keyPrefix + '[description]'] = $(this).closest('tr').find('.description').text();
postData[keyPrefix + '[quantity_input]'] = $(this).closest('tr').find('.quantity_input').val();
postData[keyPrefix + '[cost_of_items]'] = $(this).closest('tr').find('.cost_of_items').text();
postData[keyPrefix + '[cost_total_td]'] = $(this).closest('tr').find('.cost_total_td').text();
});
$.ajax
({
type: "POST",
url: "updateorder.php",
dataType: "json",
data: postData,
cache: false,
success: function()
{
alert("Order Updated");
}
});
});
好了一些进展。它现在不会将所有字段设置为“1”或者order_id是什么,但它不会更改字段中的任何值?我可以用什么其他代码来使这更容易? – benhowdle89 2010-11-12 10:10:28
你确定'$ POST ['data']'实际上是一个数组吗?尝试'var_dump($ _POST ['data']);',甚至是'var_dump($ _ POST);'。 – 2010-11-12 10:20:25
这是PHP响应:阵列(2){ [ “ORDER_ID”] => 串(1) “2” [0] => 阵列(7){ [ “supp_short_code”] => 串(0) “” [ “project_ref”] => 串(0) “” [ “om_part_no”] => 串(8) “16511316” [ “描述”] => 串(16)“ EarthTerminal6mm” [ “quantity_input”] => 串(1) “2” [ “cost_of_items”] => 串(4) “2.34” [ “cost_total_td”] => 串(4) “2.34” } } – benhowdle89 2010-11-12 10:26:48