更新购物车的会话变量

发布于 11-17 07:49 字数 2353 浏览 2 评论 0原文

我是 php 的新手(只使用了几个月),我正在为一个朋友构建一个非常简单的电子商务网站,我正在为其构建一个同样简单的购物车。

以下是购物车的基本思想:当前,用户浏览产品页面(基于 ProductID),选择商品的颜色(选择 ProdOptID,它对于每个商品都是唯一的),然后将该信息提交到 $ _SESSION['Cart'] 通过表单发布。然后,该信息用于访问 mysql 数据库,以提取所有其余的产品信息,然后将其显示在迷你购物车中。到目前为止,它运行良好。只有当我尝试创建评论/编辑页面时,我才会被绊倒。由于某些原因,我只能更新最后一篇。有人有任何想法或发现任何问题吗?

评论/编辑页面的代码:

<div class="ReviewCheckout_Main">
<h1 class="ReviewCheckout_Title">Review Your Cart</h1>
<div class="ReviewCheckout_Form">
    <form action="/BeautifulUrns/includes/ReviewCheckout_Processing.php" method="post">
        <?php
            if (isset($_SESSION['Cart'])) {
                foreach ($_SESSION['Cart'] as $ID => $QTY){
                    $Query = "SELECT *
                        FROM ProductsOptions
                        LEFT OUTER JOIN Products
                        ON ProductsOptions.ProductID=Products.ProductID
                        WHERE ProdOptID = $ID";
                    $Result = mysql_query($Query, $Connection);
                    $Row = mysql_fetch_array($Result);

                    echo "<input type='text' name='Edit_Qty' value=" . $QTY . " />";
                    echo "<b>" . $Row['ProductName'] . "</b> ";
                    echo "<b> - " . $Row['POName'] . "</b> ";
                    echo "<input type='hidden' name='Edit_ID' value='{$Row['ProductID']}' />";
                    echo "<input type='hidden' name='Edit_ProdOptID' value='{$Row['ProdOptID']}' />";
                    echo "<br />";
                }
            }
        ?>

        <input type="submit" />
    </form>
    <a href="/BeautifulUrns/index.php">Return Home</a>
</div>    

处理页面的代码:

<?php session_start();
$ID = mysql_real_escape_string((int)$_POST['Edit_ProdOptID']); // GRABS PRODOPT ID
$PRODUCTID = mysql_real_escape_string((int)$_POST['Edit_ID']);  //GRABS PRODUCT ID
$QTY = mysql_real_escape_string((int)$_POST['Edit_Qty']); // GRABS QUANTITY            

if(isset($ID)) {
   if (isset($QTY) && $QTY > 0){
    if (isset($_SESSION['Cart'])) {
        $_SESSION['Cart'][$ID] = $QTY;
    }
    else {
        $_SESSION['Cart'] = NULL;
        $_SESSION['Cart'][$ID] = $QTY;
        }
    }
}
?>

I'm new to php (have only been playing around with it for a couple of months) and I am building a friend a really simple e-commerce website for which I am building an equally simple shopping cart.

Here's the basic idea of the cart: Currently, the user browses the products page (based off of productID), selects a color for an item (which selects ProdOptID, which is unique to every single item), and then submits that information to $_SESSION['Cart'] via posting through a form. That information is then used to access a mysql DB to pull out all the rest of the product information, which then gets displayed in a mini-cart. Up to that point, it works fine. It is only when I try to create a review/edit page where I get tripped up. For some reason, I can only update the last one. Anyone got any ideas or spot any problems?

The code for the review/edit page:

<div class="ReviewCheckout_Main">
<h1 class="ReviewCheckout_Title">Review Your Cart</h1>
<div class="ReviewCheckout_Form">
    <form action="/BeautifulUrns/includes/ReviewCheckout_Processing.php" method="post">
        <?php
            if (isset($_SESSION['Cart'])) {
                foreach ($_SESSION['Cart'] as $ID => $QTY){
                    $Query = "SELECT *
                        FROM ProductsOptions
                        LEFT OUTER JOIN Products
                        ON ProductsOptions.ProductID=Products.ProductID
                        WHERE ProdOptID = $ID";
                    $Result = mysql_query($Query, $Connection);
                    $Row = mysql_fetch_array($Result);

                    echo "<input type='text' name='Edit_Qty' value=" . $QTY . " />";
                    echo "<b>" . $Row['ProductName'] . "</b> ";
                    echo "<b> - " . $Row['POName'] . "</b> ";
                    echo "<input type='hidden' name='Edit_ID' value='{$Row['ProductID']}' />";
                    echo "<input type='hidden' name='Edit_ProdOptID' value='{$Row['ProdOptID']}' />";
                    echo "<br />";
                }
            }
        ?>

        <input type="submit" />
    </form>
    <a href="/BeautifulUrns/index.php">Return Home</a>
</div>    

The Code for the processing page:

<?php session_start();
$ID = mysql_real_escape_string((int)$_POST['Edit_ProdOptID']); // GRABS PRODOPT ID
$PRODUCTID = mysql_real_escape_string((int)$_POST['Edit_ID']);  //GRABS PRODUCT ID
$QTY = mysql_real_escape_string((int)$_POST['Edit_Qty']); // GRABS QUANTITY            

if(isset($ID)) {
   if (isset($QTY) && $QTY > 0){
    if (isset($_SESSION['Cart'])) {
        $_SESSION['Cart'][$ID] = $QTY;
    }
    else {
        $_SESSION['Cart'] = NULL;
        $_SESSION['Cart'][$ID] = $QTY;
        }
    }
}
?>

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

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

发布评论

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

评论(2

时间你老了2024-11-24 07:49:04

看起来好像你的 html 输入名称都完全相同,因此最后一个会覆盖前一个。您应该做的是将 id 添加到输入名称,例如

echo "<input type='hidden' name='Edit_ID_{$Row['ProductID']}' value='{$Row['ProductID']}' />"

然后您应该使用变量变量从每个输入字段检索数据。
http://php.net/manual/en/language.variables.variable.php

It seems as though your html input names are all exactly the same, thus the last one overides the previous ones. What you should do is add an id to the input name e.g.

echo "<input type='hidden' name='Edit_ID_{$Row['ProductID']}' value='{$Row['ProductID']}' />"

Then you should use variable variables to retrieve your data from each input field.
http://php.net/manual/en/language.variables.variable.php

芸娘子的小脾气2024-11-24 07:49:04

您可以创建临时表来存储用户选择的项目,当订单最终确定时,您可以将所有记录上传到您的查询或订单表。

使用临时表,您可以轻松更新或删除任何记录

表:temporary_order

Temp_id: $_SESSION['temp_id'] = 任意随机值;

id  userid   product_id  quantity temp_id   date
1   1        1            2        123456    2011-06-27 
2   1        2            1        123456    2011-06-27
3   2        1            2        789456    2011-06-27 
4   2        2            3        789456    2011-06-27

您只需要传递 temp_id 即可从临时表中获取用户选择的记录

You can create temporary table to store user selected items and when order get finalized you can upload all records to your inquiry or order table.

Using temporary table you can easily update or delete any records

Table: temporary_order

Temp_id: $_SESSION['temp_id'] = any random value;

id  userid   product_id  quantity temp_id   date
1   1        1            2        123456    2011-06-27 
2   1        2            1        123456    2011-06-27
3   2        1            2        789456    2011-06-27 
4   2        2            3        789456    2011-06-27

You only need to pass temp_id to get user selected record from temporary table

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