Mysql编辑用户已下达的订单

嗨,我正在通过一个订购系统,我已完成订单并且它进入数据库很好,但我很难想到如何让用户在插入后编辑订单。

这就是我从页面获取订单并将其发送到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()); } } 

所以我知道这不是最干净的方法,所以这就是为什么我很难找到一种干净的方式让他们编辑订单。 我知道如何做“更新”查询,但事实上我已经用于每个循环和数组插入订单? 任何人都可以从上面的代码中获得有关向用户呈现内容的任何建议吗?

根据您正在使用的框架(如果有的话),只需为订单创建一个视图。 您可以在其他页面上执行此操作,也可以通过ajax加载表单。 无论哪种方式,表单看起来像这样:

 update("orders",$_POST,$_POST['id']); } $order = $database->query( "select id, order_id, project_ref, supp_short_code, om_part_no, description, quantity, cost_of_items, cost_total from orders where id = ".$id); ?> 

$ database对象的代码在这里:

http://www.jtgraphic.net/code/database-object/

我使用第三方代码生成项目创建一个用于插入/更新记录的数据网格。 他们所做的是每次有更新时为数据网格中的每个项运行更新查询。 这确实会导致发生一些不必要的更新。

我认为更简洁的方法是检查旧数据集和新数据集之间的差异,然后仅对那些已更改的项运行更新。

我应该写一些类似“这是邪恶的,不要那样做”的东西,但我只会建议[强烈!]阅读SQL注入和[这里稍微强一点]框架,并讨论这个问题 。

如果在数据库中设置了正确的“唯一”和“键”属性,那么如果您已经不关心安全性,则将INSERT更改为REPLACE就足够了。 (如果这是tommorow的学术作业,那就没关系了:P)

戴上我的OO帽子,这里最好的选择是将订单视为一个对象。

编写一个处理您需要处理的所有内容的类,例如创建新的,清理,validation,保存,检索,将其显示在屏幕上进行编辑和删除。 还有其他更高级别的function,如打印发票,生成装箱单等,以及这些更低级别的方法。

开始时需要做更多工作,但可维护性,可扩展性和可重用性更高。

并非所有这些都需要手工完成:查看Active Record设计模式和PHP-AR等工具,它们将为您完成大部分基本的getter和setter方法。

如果这听起来像双荷兰,请在线浏览许多OO PHP教程之一,并查看“活动记录设计模式”的wiki条目。

我意识到,这更像是对方法的建议,而不是对订单编辑问题的直接回答。

G