0

花了很长时间,但我有一个购物车工作。问题是,在我的业务性质中,客户的购物车中可能有数百件商品,而我更新单个商品的方式非常原始,如果有很多商品需要更新,可能会变得乏味。

对于客户购物车的购物车数据库中的每一行,我都会生成商品的表单和产品信息。因此,每一行都有它自己的更新项目按钮。

$top_query = "SELECT t1.product_id, image_path, suggested_quantity, sales_info sku_item_number FROM product_customer_suggested_qty AS t1 
            LEFT JOIN product_images AS t2 ON t1.product_id = t2.product_id 
RIGHT JOIN products AS t3 ON t1.product_id = t3.product_id          
            WHERE customer_id = '".$customer_id."'
            AND cart_visible != 0";
        //echo $top_query;
            $histresult = mysql_query($top_query) or die (mysql_error());

 <tr>
                <td width="181"><strong>Product</strong></td>
                <td width="181"><strong>Sales Info</strong></td>
                <td width="100"><strong>Suggested Qty</strong></td>
                <td width="100"><strong>Qty</strong></td>
                <td width="100"><strong>Image</strong></td>
                <td width="100"><strong>Add to Cart?</strong></td>
              </tr>
              <?php
              $i =0;
              while($row=mysql_fetch_array($histresult))
            {
                echo '<tr height = "50px">';
                //We grab the product id as well as everything else we need.
                $customer_id= $row['customer_id'];
                $product_id= $row['product_id'];
                $link = $row['image_path'];
                $sku_item_number = $row['sku_item_number'];
                $suggested_quantity = $row['suggested_quantity'];

                 echo '
                 <form action="ezcopy.php? method="post"><td>'.$sku_item_number.'</td>';
    echo '<td>'.$suggested_quantity.'
    <input class="same" id="same'.$i.'" title="'.$i.'" name="same'.$i.'" type="checkbox" value ="'.$suggested_quantity.'"/> 
    <input name="customer_id" type="hidden" value="'.$customer_id.'">
    <input name="product_id" type="hidden" value="'.$product_id.'">
    <input name="i" type="hidden" value="'.$i.'">
    </td>';

    echo '<td><input  class="qty" name="qty'.$i.'" id="qty'.$i.'" type="text"size="4" maxlength="4"></td>';

这只是整行的一部分,它完全可以用于更新数量,并允许用户根据他们的历史记录自动复制建议的值。

但是,如果我只想在底部制作一个显示“全部更新”的按钮,从而使整个购物车成为一个大表格,我该如何修改数量表格,以便它可以读取未知的动态数量的项目并更新数据库?实现这一目标的好策略是什么?

4

1 回答 1

0

在与谷歌苦苦挣扎之后,我终于在这里找到了一个例子。 http://www.theblog.ca/update-multiple-rows-mysql

关键是在行生成循环之外将表单更改为一个大的而不是许多小的。

 while($row=mysql_fetch_array($histresult))
            {
                echo '<tr height = "50px">';
                //We grab the product id as well as everything else we need.
                $customer_id= $row['customer_id'];
                $product_id= $row['product_id'];
                $link = $row['image_path'];
                $sku_item_number = $row['sku_item_number'];
                $suggested_quantity = $row['suggested_quantity'];
                $sales_info = $row['sales_info'];
                $item_id = $row['id_product_customer_suggested_qty'];

                 echo '<td>'.$sku_item_number;
                 //This array stores the item index.

                 echo '</td>';

                 echo '<td>'.$sales_info.'</td>';
    echo '<td>'.$suggested_quantity.'
    <input class="same" id="same'.$i.'" title="'.$i.'" name="same'.$i.'" type="checkbox" value ="'.$suggested_quantity.'"/> 
    <input name="item_id[]" type="hidden" value="'.$item_id.'">
    <input name="customer_id[]" type="hidden" value="'.$customer_id.'">
    <input name="product_id[]" type="hidden" value="'.$product_id.'">
    <input name="i" type="hidden" value="'.$i.'">
    </td>';

    echo '<td><input  class="qty" name="qty[]" id="qty'.$i.'" type="text"size="4" maxlength="4"></td>';

第一个关键是输入名称是如何改变的。它们现在都是数组。在行生成循环的末尾是一个提交按钮。然后,提交时发生的事情是下一个关键。

foreach($_POST['item_id'] as $key=>$item_id)
{ 
    $qty = $_POST['qty'][$key];
    $product_id = $_POST['product_id'][$key];
    $customer_id = $_POST['customer_id'][$key];

    if (!empty($qty))
    {
        echo 'Product is '.$product_id.'<br>';
        echo 'Quantity requestesd is '.$qty.'<br><br>';
        //perform UPDATE query as that inidividual item
        }
}

这会动态抓取表单中的每一行,每一行都由 item_id 唯一。我们现在可以只在需要时执行更新查询。这是一个非常强大的技巧。

于 2012-08-20T21:23:41.493 回答