简体   繁体   English

从数据库检索购物车项目

[英]Retrieve shopping cart items from database

I'm trying to implement a shopping cart for my website. 我正在尝试为我的网站实施购物车。 My page loops through products in my database and displays them on the page with an 'Add' button. 我的页面遍历数据库中的产品,并通过“添加”按钮将其显示在页面上。 You can only add an item if you are logged in. 如果您已登录,则只能添加一个项目。

Products Table 产品表

| ID | Name | Price | Quantity | description | image_name | 

Once you add an item it stores this in my cart table. 一旦添加项目,它将存储在我的购物车表中。 The 'CartProduct int' is the same as the 'ID' in my products table. “ CartProduct int”与我的产品表中的“ ID”相同。

Cart table 推车表

| CartID | CartProduct | ProductCount | UniqueID | Username | Date

If you click Add on the same product, the ProductCount will increment. 如果您在同一产品上单击添加,则ProductCount将增加。

So if TestUser adds CartProduct 1 & 2 and 2 again. 因此,如果TestUser再次添加了CartProduct 1&2和2。
I'm really unsure on how to display all products WHERE username 'TestUser' and I also need ProductCount * Price. 我真的不确定如何显示所有产品,用户名“ TestUser”,我还需要ProductCount * Price。

At the moment I'm trying to loop through each CartProduct for the logged in user, then select the ID from products table WHERE ID = CartProduct (as they are the same). 目前,我正在尝试遍历登录用户的每个CartProduct,然后从产品表WHERE ID = CartProduct中选择ID(因为它们相同)。 I can then grab the price and details of that product. 然后,我可以获取该产品的价格和详细信息。

At the moment I am selecting from cart where username = username that is logged in. Just so I can see the outcome, I am displaying it. 目前,我正在从购物车中选择用户名=登录的用户名。为了显示结果,我正在显示它。

$sql = "SELECT cartProduct FROM cart WHERE username ='" . $name . "'";
                    $result = mysql_query($sql);

                    $c = 0;
                    $n = 2; // Each Nth iteration would be a new table row
                    while ($db_field = mysql_fetch_assoc($result)) {
                        $res = mysql_query($sql);
                        $row = mysql_fetch_assoc($res);
                        $productNo = $row['cartProduct'];

                        if ($c % $n == 0 && $c != 0) { // If $c is divisible by $n...
                            echo '<div class="row" ></div>';
                        }
                        $c++;
                        echo "<br>Count: " .$c;
                        ?>
                        <h2> <?php echo "Product No: " . $productNo ?> </a></h2>
                        <?php

I add 3 items to the cart. 我在购物车中添加了3件商品。 CartProduct 1, 2, 2 :- for the output I keep getting: CartProduct 1,2,2:-对于输出,我不断得到:

Product No: 1 商品编号:1
Product No: 1 商品编号:1

I should see Product 1 and then Product 2? 我应该看到产品1,然后是产品2?

Am I making this more complicated than it should be, and are my tables correct? 我是否使它变得比它应该的复杂,我的表是否正确?
Is there an easier way? 有更容易的方法吗?

Many thanks for any replies! 非常感谢您的任何答复!

First, in Cart Table change Username to UserId. 首先,在购物车表中将用户名更改为UserId。 The next one step is modify sql query at "JOIN products" to get connect with Products Table and easly get a product price. 下一步是在“加入产品”处修改sql查询,以与产品表建立联系并轻松获得产品价格。

But in general, use PDO driver to get more safeties and clearlies queries and PHP code. 但是总的来说,使用PDO驱动程序可以获得更多的安全性并清除查询和PHP代码。 Read about relations in DB (RDBM). 阅读有关DB(RDBM)中的关系的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM