简体   繁体   English

如何用php中的金额更新购物车数量?

[英]How to update shopping cart quantity with amount in php?

Below is my not working source code? 以下是我无法正常工作的源代码? I want to update the quantity with respective to that perticular item and calculating total. 我想更新与该特定项目的数量并计算总数。 I was stuck or did wrong coding. 我被卡住或编码错误。 Please can anyone tell me what to do furture ? 请谁能告诉我做什么? means when user increasing quantity price of that item should be incremented.. 表示当用户增加该项目的数量价格时应增加。

         $sel_user = "select * from cart where ip_add = '$ip' ";

            $run_sel_user = mysqli_query($con,$sel_user);

            while($p_price = mysqli_fetch_array($run_sel_user))
            {
                $pro_id = $p_price['p_id'];

                $sel_pro_from_product = "select * from products where product_id = '$pro_id'";

                $run_pro_from_product = mysqli_query($con,$sel_pro_from_product);

                while($pro_price = mysqli_fetch_array($run_pro_from_product))
                {

                    $product_price = array($pro_price['product_price']);

                    $product_title = $pro_price['product_title'];

                    $product_image = $pro_price['product_img1'];

                    $product_single_price = $pro_price['product_price'];

                    $values = array_sum($product_price);

                    $total += $values ;
                    if(isset($_POST['update_cart']))
                    {
                        if(isset($_POST['qty']))
                        {
                            print_r($_POST['qty']);
                            foreach($_POST['qty'] as $qty_add)
                            {

                                $qty = $qty_add;
                                $update_qty = "update cart set qty='$qty' where p_id='$pro_id'";
                                $run_qty = mysqli_query($con,$update_qty);
                                $total=$total*$qty;
                            }
                        }
                        else
                        {

                        }
                    }

            ?>
            <tr align="center" >
                <td style="padding:5px; border:1px solid black;"><input type="checkbox" name="remove[]" value="<?php echo $pro_id;?>"/></td>

                <td style="padding:5px; border:1px solid black;"><?php echo $product_title;?><br>
                    <img src="admin_area/product_images/<?php echo $product_image; ?>" height="60" width="60"/>
                </td>
                <td style="padding:5px; border:1px solid black;"><input type="text" size="4" name="qty[]" 
                    value="<?php
                            $default_qty = 1;
                            if(!isset($_POST['qty']))
                            {
                                echo $default_qty;
                            }


                    ?>" style="text-align:center;"/></td>


                        <td style="padding:5px; border:1px solid black;"><?php echo $product_single_price." Rs ";?></td>
                    </tr>
                    <?php   
                }}

                    ?>

You are setting the name of your input box as a qty[] array, and both looping for each item in the cart and each item in the array So if you have 20 items in the cart, you are currently sending 20x20 queries, and 19 out of the 20 would contain the wrong info. 您正在将输入框的名称设置为qty []数组,并且都针对购物车中的每个项目和该数组中的每个项目进行了循环。因此,如果购物车中有20个项目,则当前正在发送20x20查询和19 20个中包含错误的信息。 Instead you can set the name of the input box using $pro_id variable. 而是可以使用$ pro_id变量设置输入框的名称。

<input type="text" size="4" name="qty<?php echo($pro_id);?>"/>

This way each of your textbox will have its own unique name, and that name identifies which product it is. 这样,您的每个文本框都有其自己的唯一名称,该名称标识了它是哪种产品。 Then in your while loop above you could do something like this 然后在上面的while循环中,您可以执行以下操作

if(isset($_POST['update_cart']))
{
    if(isset($_POST['qty' . $pro_id]))
    {
        $quantity = (int)$_POST['qty' . $pro_id];
        $run_qty = mysqli_query($con,"update cart set qty='$qty' where p_id='$pro_id'");
    }
}

But of course, the way it's setup right now, there is no way to know which records in the cart table belongs to which users. 但是,当然,按照现在的设置方式,无法知道购物车表中的哪些记录属于哪些用户。 So this would not work in the real world setup without more changes (like a field identifying which records belong to which user) 因此,如果不进行更多更改(例如,标识哪些记录属于哪个用户的字段),这在现实世界的设置中将不起作用。

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

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