Mysql 编辑用户下的订单

发布于 2024-09-30 23:33:54 字数 2355 浏览 7 评论 0原文

您好,我正在使用一个订购系统,我已经完成了下订单并将其很好地存入数据库,但我正在努力思考如何让用户在插入订单后编辑订单。

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

所以我知道这不是最干净的方法,所以这就是为什么我正在努力寻找一个让他们编辑订单的干净方式。我知道如何进行“更新”查询,但事实上我已经使用每个循环和数组来插入订单?对于上面的代码,有人对向用户展示什么有什么建议吗?

Hi i'm working through an ordering system, i've got the placing an order done and its going into the database fine, but i'm struggling to think how to let the user edit the order once its been inserted.

This is how i grab the order from the page and send it to a PHP insert script:

$('#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");
                        }
                    });
            });

And this is the PHP insert:

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());
                }
            }

So i'm aware this isnt the cleanest way to do it, so thats why i'm struggling to find a clean way to let them edit the order. I know how to do "Update" queries but its the fact that i've used for each loops and arrays to insert the order? Anyone have any advice from the code above on what to present to the user?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

揽清风入怀 2024-10-07 23:33:54

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

<?php
    if(isset($_POST['submit']) && $_POST['submit'] == "Submit") {
        $database->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);
?>

<form method="post" action="<?php echo basename($_SERVER['PHP_SELF']); ?>">
    <input type="text" name="id"                value="<?php echo $order['id']; ?>">
    <input type="text" name="order_id"          value="<?php echo $order['order_id']; ?>">
    <input type="text" name="project_ref"       value="<?php echo $order['project_ref']; ?>">
    <input type="text" name="supp_short_code"   value="<?php echo $order['supp_short_code']; ?>">
    <input type="text" name="om_part_no"        value="<?php echo $order['om_part_no']; ?>">
    <input type="text" name="description"       value="<?php echo $order['description']; ?>">
    <input type="text" name="quantity"          value="<?php echo $order['quantity']; ?>">
    <input type="text" name="cost_of_items"     value="<?php echo $order['cost_of_items']; ?>">
    <input type="text" name="cost_total"    value="<?php echo $order['cost_total']; ?>">
    <input type="submit" name="submit" value="Submit">
</form>

$database 对象的代码在这里:

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

Depending on the framework you're using (if one at all), just create a view for the order. You could either do this on another page or by loading a form via ajax. Either way, the form would look something like this:

<?php
    if(isset($_POST['submit']) && $_POST['submit'] == "Submit") {
        $database->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);
?>

<form method="post" action="<?php echo basename($_SERVER['PHP_SELF']); ?>">
    <input type="text" name="id"                value="<?php echo $order['id']; ?>">
    <input type="text" name="order_id"          value="<?php echo $order['order_id']; ?>">
    <input type="text" name="project_ref"       value="<?php echo $order['project_ref']; ?>">
    <input type="text" name="supp_short_code"   value="<?php echo $order['supp_short_code']; ?>">
    <input type="text" name="om_part_no"        value="<?php echo $order['om_part_no']; ?>">
    <input type="text" name="description"       value="<?php echo $order['description']; ?>">
    <input type="text" name="quantity"          value="<?php echo $order['quantity']; ?>">
    <input type="text" name="cost_of_items"     value="<?php echo $order['cost_of_items']; ?>">
    <input type="text" name="cost_total"    value="<?php echo $order['cost_total']; ?>">
    <input type="submit" name="submit" value="Submit">
</form>

The code for the $database object is here:

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

冰雪之触 2024-10-07 23:33:54

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

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

I use a third party code-generation project that creates a data grid that is used for inserting/updating records. What they do is run the update query for each item in the data grid everytime there is a update. This does result in some unnecessary updates occuring.

I think that a cleaner method would be to check the difference between the old data set and the new data set and then run the update only for those items that are changed.

情感失落者 2024-10-07 23:33:54

我应该写一些类似“这是邪恶的,不要那样做”之类的东西,但我只会建议[强烈!]阅读 SQL 注入 和[此处强度稍弱]讨论的框架此处并开始处理您的案例。

如果您在数据库中设置了正确的“unique”和“key”属性,如果您已经不关心安全性,则将 INSERT 更改为 REPLACE 可能就足够了。 (所以如果这是明天的学术作业也没关系:P)

I should write something like "this is evil, don't do that" and stuff, but I'll only suggest [strongly!] reading on SQL injection and [a bit less strength here] frameworks discussed here and get to your case.

If you set correct "unique" and "key" attributes in database it might be enough to change INSERT to REPLACE if you already don't care about safety. (So it's ok if it's an academic homework for tommorow :P)

书间行客 2024-10-07 23:33:54

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

编写一个类来处理您需要用它执行的所有操作,例如创建新的、清理、验证、保存、检索、将其显示到屏幕上进行编辑和删除。还有其他更高级别的功能,例如打印发票、生成装箱单等,以及这些较低级别的方法。

一开始需要做更多的工作,但可维护性、可扩展性和可重用性却无限提高。

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

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

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

G

Putting my OO hat on, your best bet here is to treat the order as an object.

Write a class that handles everything you need to do with it, such as creating new, sanitising, validating, saving, retrieving, displaying it to the screen to edit, and deleting. There'll be other higher-level functions such as printing invoices, generating packing slips, etc, as well as these more lower-level methods.

More work to begin with, but infinitely more maintainable, extensible and reuseable.

Not all of this need be done by hand: have a look at the Active Record design pattern and tools like PHP-AR that will do most of the basic getter and setter methods for you.

If this sounds like double-dutch, have a browse for one of the many OO PHP tutorials online & have a look at the wiki entry for 'active record design pattern'.

This is more of a suggestion for an approach than a direct answer to the order editing issue, I realise.

G

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文