结账后删除数据行

时间:2016-01-02 14:46:02

标签: php mysql

我想在客户决定结账后删除cart表上的数据。基本上,我希望将其从表中删除的原因是重置所有总项目和总价格。

到目前为止,我已尝试在$run_sales = mysqli_query($con, $insert_sales);

之后添加此内容

if($run_sales){ mysqli_query($con, "delete * from cart where orderId = '$sales_itemid'"); }

我提供了一个会显示错误的else语句,但似乎没有出错。但该行未被删除。我需要在哪里和什么地添加我的代码呢?

<?php 
     $con = mysqli_connect("localhost","root","","ecommerce");
        if(isset($_SESSION['customer_email'])){ 
        $user_login = "select customer_id from customer where customer_email ='"
         .  $_SESSION['customer_email'] .
          "'"  or die(mysqli_error($con));
        $run_login = mysqli_query($con, $user_login); 
            while($p_price=mysqli_fetch_array($run_login)){

            $_SESSION['user_id'] = $p_price['customer_id']; 
            }   
                    $join_query = "select customer.customer_email, item.ItemId, item.ItemPrice, cart.qty 
                    from customer, item, cart
                    where customer.customer_email = '" . $_SESSION['customer_email'] . "' AND item.ItemId = cart.orderId";

                    $run_join = mysqli_query($con, $join_query);
                    //checks error if(!$run_join)
                    if(!$run_join){
                        printf("ERROR: %s\n", mysqli_error($con));
                        exit();
                    }   //END OF if(!$run_join)

                    //insert every type of item the customer has bought.
                    while($row_boat = mysqli_fetch_array($run_join)){

                        $sales_email = $_SESSION['customer_email'];
                        $sales_itemid = $row_boat['ItemId'];
                        $sales_itemprice = $row_boat['ItemPrice'];
                        $sales_qty = $row_boat['qty'];
                        $sales_total = $sales_itemprice * $sales_qty;
                        //$sales_date =
                        $insert_sales = "insert into sales (customer_email, ItemId, ItemPrice, Quantity, Total)
                         values ('$sales_email', '$sales_itemid', '$sales_itemprice', '$sales_qty', '$sales_total')";

                        $run_sales = mysqli_query($con, $insert_sales); 
                        if($run_sales){
                            mysqli_query($con, "delete * from cart where orderId = '$sales_itemid'");
                        }
                        else
                        {
                            printf("ERROR: %s\n", mysqli_error($con));
                        exit();
                        }
                    }
                }
        //echo $_SESSION['user_id'];
         ?>

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

if($run_sales == TRUE){
                        mysqli_query($con, "delete * from cart where orderId = '$sales_itemid'");
                    }

或者:

if($run_sales){
$count = mysqli_affected_rows($mysqli);
if($count>0){
    mysqli_query($con, "delete * from cart where orderId = '$sales_itemid'");
}
else{

如果一切都不起作用,请尝试:

if($run_sales->num_rows > 0){