如何更新购物车数量小计?

时间:2015-05-13 10:27:03

标签: php sql

在我向我选择的某个产品中添加了一个数量之后,按下更新按钮后,小计没有给我一个正确的结帐总额,只有在计算它们时显示错误的最终总数时才会读取单个数量。

            <table align="center" width="700" bgcolor="white">

                <tr align="center">
                    <th>Remove</th>
                    <th>Product(S)</th>
                    <th>Quantity</th>
                    <th>Total Price</th>
                </tr>

    <?php 
    $total = 0;

    global $connection; 

    $ip = getIp(); 

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

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

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

        $pro_id = $p_price['p_id']; 

        $pro_price = "select * from Product where Product_ID='$pro_id'";

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

        while ($pp_price = mysqli_fetch_array($run_pro_price)){

        $product_price = array($pp_price['Price']);

        $product_title = $pp_price['Product_Name'];

        $product_image = $pp_price['Image']; 

        $single_price = $pp_price['Price'];

        $values = array_sum($product_price); 

        $total += $values; 

                ?>

                <tr align="center">
                    <td><input type="checkbox" name="remove[]" value="<?php echo $pro_id;?>"/></td>
                    <td style="border:1; color:black;"><?php echo $product_title; ?><br>
                    <img src="admin_area/product_images/<?php echo $product_image;?>" width="60" height="60"/>
                    </td>
                    <td><input type="text" size="4" name="qty" value="<?php echo $_SESSION['qty'];?>"/></td>

                    <?php 

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

                        $qty = $_POST['qty'];

                        $update_qty = "update cart set qty='$qty'";

                        $run_qty = mysqli_query($connection, $update_qty); 

                        $_SESSION['qty']=$qty;

                        $total = $total*$qty;
                    }


                    ?>


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


            <?php } } ?>






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

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

            </table> 

        </form>

我认为问题出在这里(不确定)

<?php 

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

                            $qty = $_POST['qty'];

                            $update_qty = "update cart set qty='$qty'";

                            $run_qty = mysqli_query($connection, $update_qty); 

                            $_SESSION['qty']=$qty;

                            $total = $total*$qty;
                        }


                        ?>

以下是实际页面的链接和我的数据库的另一个链接

http://www.ya-techno.com/up/uploads/1430742773071.png http://www.ya-techno.com/up/uploads/1430742841161.png

1 个答案:

答案 0 :(得分:0)

你可以使用jquery ajax post方法来提取它们......

使用下面的php代码提取购物车单个小计:

$this->cart->contents()['eccbc87e4b5ce2fe28308fd9f2a7baf3']['subtotal']