购物车:从购物车更新和删除产品

时间:2017-10-20 22:06:01

标签: php html shopping-cart

嘿,我试图建立一个网上购物网站,但我卡在购物车部分

我试图在文本框中显示产品数量并更新它,这似乎不适用于我的代码,也无法从购物车中删除产品。

使用复选框进行删除,使用提交类型的更新按钮和显示数量的文本框

这是我的cart.php页面的代码..

<form action="" method="post" enctype="multipart/form-data">

    <table align="center" width="700px" bgcolor="skyblue">

    <tr align="center">
        <th>Remove</th>
        <th>Products</th>
        <th>Quantity</th>
        <th>Total Price</th>
    </tr>

<?php 

$total = 0;

global $con;

$ip = getIp();

$select_price = "select * from cart where ip_addr = '$ip'";

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

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

    $pro_id = $p_price['p_id'];
    $pro_qty = $p_price['qty'];

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

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

    $select_qty = "select * from cart where p_id = '$pro_id'";

    $run_qty = mysqli_query($con, $select_qty);

    $row_qty = mysqli_fetch_array($run_qty);

    $qty = $row_qty['qty'];

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

        $product_price = array($pp_price['product_price']);
        $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 echo 
$pro_id; ?>"></td>
        <td><?php echo $product_title; ?><br>
        <img src="Admin_area/product_image/<?php echo $product_image; ?>" 
width="60px" height="60px">
        </td>
        <td><input type="text" name="qty" size="4px" value="<?php echo $qty; 
?>"></td>  // Calling the $qty variable which store the product quantity

        <?php

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

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

            $_SESSION['qty'] = $qty;

            $total = $total*$qty;

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

        }

        ?>

        <td>&#8360 <?php echo $single_price; ?></td> 
    </tr>

    <?php } } ?>

    <tr align="right">
        <td colspan="2" style="padding: 10px;"><b>Sub Total:</b></td>
        <td colspan="2" style="padding: 10px;">&#8360 <?php echo $total; ?>
</td>
    </tr>

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


    </table>

    </form>

    <?php 

    function updatecart(){

    $ip = getIp();

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

 // when update cart button clicked delete all product which are checked

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

            $delete_product = "delete from cart where p_id = '$remove_id' 
AND ip_addr = '$ip'";

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

            if ($run_delete) {

        echo "<script>alert('Inside Function');</script>";


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

        }

    }

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

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

    }

}

echo @$update_cart = updatecart();

?>          

    </div>

0 个答案:

没有答案