Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php mysql购物车更新数量错误_Php_Mysql - Fatal编程技术网

php mysql购物车更新数量错误

php mysql购物车更新数量错误,php,mysql,Php,Mysql,我对编码和尝试更新购物车中的数量还不熟悉,但它只接受一种产品 如果我添加多个产品,我只能更新最后一个产品,这也会将所有其他产品设置为相同的数量 我需要帮忙把数量分开 这是我的密码: <form action="" method="post" enctype="multipart/form-data"> <table align="center" width="700" > <tr align="center"

我对编码和尝试更新购物车中的数量还不熟悉,但它只接受一种产品

如果我添加多个产品,我只能更新最后一个产品,这也会将所有其他产品设置为相同的数量

我需要帮忙把数量分开

这是我的密码:

<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>";

        }


    ?>

购物车
去除
产品
量
价格
总价

看起来您正在将HTML表单控件命名为“PHP数组”<代码>

这些表单元素名称只能是字母数字,在服务器上,您可以通过例如将产品的id后固定到表单控件名称并在那里进行检查来区分这些元素


顺便说一下,我建议使用
$\u SESSION
进行购物车处理

警告:使用
mysqli
时,应使用参数化查询,并将用户数据添加到查询中。不要使用字符串插值或串联来完成此操作,因为这样会创建严重的错误。切勿将
$\u POST
数据直接放入查询中。您使用的是什么产品?购物车的内容是什么<代码>$update\u qty=“更新购物车设置数量='$qty'你不是想把它调用到一个驻留在
购物车中的产品上吗?购物车表有一个'p_id'列,它从产品表中获取产品id,其次是'ip_add',它获取ip地址,最后是'qty',它是数量。我对会话不太熟悉,让我试试,有什么建议吗