简体   繁体   English

php mysql购物车更新数量错误

[英]php mysql shopping cart updating quantity error

I am new to coding and trying to update quantities in my cart, but it only accepts for one product.我不熟悉编码并尝试更新购物车中的数量,但它只接受一种产品。

If I add more than one, I can only update the last product, which will also set all the other products to the same quantity.如果我添加多个,我只能更新最后一个产品,这也会将所有其他产品设置为相同数量。

I need help separating the quantities.我需要帮助分离数量。

Here is my code:这是我的代码:

<form action="" method="post" enctype="multipart/form-data"> 
            <table align="center" width="700" >
                <tr align="center">
                    <td  colspan="5"><h2>SHOPPING CART</h2></td>
                </tr>
                <tr align="center">
                    <th>Remove</th>
                    <th>Product (s)</th>
                    <th>Quantity</th>
                    <th> Price</th>
                    <th> Total Price</th>
                </tr>

                <?php 

                global $con;
        $total = 0;
        $ip = getIp();


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

        $run_price = mysqli_query($con,$sel_price);

        while($p_price = mysqli_fetch_array($run_price)){

            $pro_id = $p_price['p_id'];

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

            $run_pro_price = mysqli_query($con, $pro_price);

            while($pp_price = mysqli_fetch_array($run_pro_price)){
                $product_price = array($pp_price['product_price']);

                $product_id = $pp_price['product_id'];
                $product_title = $pp_price['product_title'];
                $product_image = $pp_price['product_image'];

                $single_price = $pp_price['product_price'];

                $values = array_sum($product_price);

                $total += $values;

                ?>
            <tr align = "center">
                <td><input type="checkbox" name="remove[]" value=" <?php  global $con; echo $pro_id; ?>"/></td>
                <td><span style="color: white;"><?php echo $product_title; ?></span>
                <br>
                <img src="admin_area/product_images/<?php  echo $product_image; ?>" width="100px" height="100px"></td>
                <td><input type="number" size="4" name="qty" /></td>
                <?php
                global $con;
                global $Stotal;
                $Stotal = $single_price;

                if(isset($_POST['update_quantity'])){

                    $qty = $_POST['qty'];
                    $update_qty = "update cart set qty='$qty' ";
                    $run_qty = mysqli_query($con, $update_qty);


                    $Stotal = $single_price * $qty;

                    }




                ?>
                <td><?php echo "KSh. " . $single_price; ?></td> 
                    <td><?php echo "KSh. " . $Stotal; ?></td>
            </tr>

        <?php }} ?>
        <tr align="right">
                <td colspan="5"><b>Total:</b> <?php echo "KSh. " . $total; ?></td>      
        </tr>


        <tr align="center">
            <td><input type="submit" name="update_cart" value="Update Cart" /></td>
            <td><input type="submit" name="continue" value="Continue Shopping" /></td>
            <td><input type="submit" name="update_quantity" value="Update Quantity" /></td>
            <td><button><a href="checkout.php" style="text-decoration: none; color: black;"> Checkout </a></button></td>
        </tr>
            </table>
        </form>
    <?php

        global $con;
        $ip = getIp();

        if(isset($_POST['update_cart'])){

            foreach($_POST['remove'] as $remove_id){

            $delete_pro = "delete from cart where p_id = '$remove_id' and ip_add='$ip'";

            $run_delete = mysqli_query($con, $delete_pro);

            if($run_delete){
                echo "<script>window.open('cart.php','_self')</script>";

            }
            }

        }

        if(isset($_POST['continue'])){

        echo "<script>window.open('index.php','_self')</script>";

        }


    ?>

Looks like you are naming HTML form controls as 'PHP arrays'.看起来您将 HTML 表单控件命名为“PHP 数组”。 <input name="remove[]">

These form element names can only be alphanumeric, and on the server you can differentiate the elements by for instance postfixing the id of the product to the form control name and checking it there.这些表单元素名称只能是字母数字,在服务器上,您可以通过例如将产品的 id 后缀到表单控件名称并在那里进行检查来区分元素。

By the way, I recommend using $_SESSION for cart handling顺便说一句,我建议使用$_SESSION进行购物车处理

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

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