【发布时间】:2011-05-06 14:22:49
【问题描述】:
您好,我正在通过一个订购系统工作,我已经完成了下单,并且可以正常进入数据库,但我正在努力思考如何让用户在插入订单后对其进行编辑。
这就是我从页面中获取订单并将其发送到 PHP 插入脚本的方式:
$('#submit').live('click',function(){
var postData = {};
$('#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: "order.php",
dataType: "json",
data: postData,
cache: false,
success: function()
{
alert("Order Submitted");
}
});
});
这是 PHP 插入:
if (isset($_POST['data']) && is_array($_POST['data'])) {
foreach ($_POST['data'] as $row => $data) {
$result = mysql_query("INSERT INTO orders (id,order_id,project_ref,supp_short_code,om_part_no,description,quantity,cost_of_items,cost_total) VALUES('', '".$order_id."', '".$data['project_ref']."', '".$data['supp_short_code']."', '".$data['om_part_no']."', '".$data['description']."', '".$data['quantity_input']."', '".$data['cost_of_items']."', '".$data['cost_total_td']."') ") or die(mysql_error());
}
}
所以我知道这不是最干净的方法,所以我一直在努力寻找一种干净的方法让他们编辑订单。我知道如何进行“更新”查询,但事实上我已经使用每个循环和数组来插入订单?有人对上面的代码有什么建议可以向用户展示什么吗?
【问题讨论】:
-
您应该清理要插入的值,而不是将原始帖子数据直接输入 sql。查看
mysql_real_escape_string()。 -
注意到了。我本来打算最后做的,只是让功能先行:)