mysql代码接受客户订单而购物车中没有产品?

fdx2calv  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(275)

我用php和mysql开发了一个网上购物网站。当客户下订单时,首先我要检查他的购物车里是否有任何产品。如果没有产品,他就不能下订单并向他显示购物车已空的消息,然后将他重定向到主页。但今天有人不知怎的下了一个订单,他的购物车里没有一件商品。这怎么可能?代码有时可能无法准确执行吗?如果不是的话,我的案子怎么会这样?
我张贴我的代码行,我在保存客户订单使用。

<?php
session_start();
include "connection.php";

if (isset($_POST['submit'])){
$cutomer_id=$_SESSION['customer_id'];
$customer_address=$_POST['delivery_address'];
$customer_name=$_POST['customer_name'];
$session_id= session_id();

//IF CART EMPTY THEN REJECT ORDER
$strSql= "select * from cart where session_id='" .$session_id."'";

    $result= mysqli_query ($con,$strSql);
    //if nothing found in cart then opt out
    $count_prods = mysqli_num_rows($result);

                            if($count_prods==0){

                            echo "Your cart is empty";
                            exit;
                            }
//UPDATE THE DELIVERY ADDRESS OF THE CUSTOMER
$update_address="UPDATE user_info SET address1='$customer_address', first_name='$customer_name' WHERE user_id='$cutomer_id'";

$success= mysqli_query($con,$update_address);

//making order number
$strsql2="SELECT NEXTval('order_number_producer') as order_number";
    $result2=mysqli_query($con,$strsql2);
    $got_it=mysqli_fetch_assoc($result2);
    $order_number=$got_it['order_number'];

//first insert into new orders

date_default_timezone_set("Asia/Delhi");
$orderTime=date("d-m-Y h:i:s A");

$strsql3="INSERT INTO new_order (order_number, customer_id, order_status,delivery_date, delivery_time,order_time) VALUES ('$order_number','$cutomer_id','processing','$delivery_date','$delivery_time','$orderTime')";
$successfull= mysqli_query($con,$strsql3);

//Now insert order details into order_details table

$strSql= "select * from cart where session_id='" .$session_id."'";

    $result= mysqli_query($con,$strSql);
    while ($rows= mysqli_fetch_assoc($result)){
    $p_id       =$rows['p_id'];
    $qty    = $rows['qty'];
    $price  = $rows['price'];

$strsqlOrderDetails="INSERT INTO order_details(order_number, product_id, qty, price) VALUES ('$order_number','$p_id','$qty','$price')";
    $done= mysqli_query($con,$strsqlOrderDetails);

}

//New order created, ordered products inserted...NOW CLEAR THE CART OF   CUSTOMER
$clearcart=$strSql= "delete from cart where session_id='" .$session_id."'";

    $cleared= mysqli_query($con,$clearcart);
echo "Order submitted. Your order number is # $order_number";

}
?>
xam8gpfp

xam8gpfp1#

听起来可能是臭名昭著的按钮双击问题。看起来订单在第一次点击时就通过了,结果购物车被清空了。
然后第二次点击(在双击中)没有找到订单?
也许可以通过双击按钮来模拟这种行为,如果这是罪魁祸首,可以添加一些javascript来防止双击按钮。

相关问题