我正在用PHP创建电子商务网站。我现在卡在购物车里。当不同IP地址的用户添加已由其他用户添加的产品时,该产品不会进入我的数据库的购物车表。对于。进出口。如果user1在数据库中添加了“Book 1”,那么user2无法在数据库中添加“Book1”,因为Book1已经由user1添加。所以我试图解决这个错误,但它没有得到解决。
//购物车功能:
function cart()
{
if(isset($_GET['add_cart']))
{
global $con;
$ip = getIp();
$cb_id = $_GET['add_cart'];
$cart_query = "select * from cart where ip_add='$ip' AND cb_id='$cb_id'";
$run_query = mysqli_query($con, $cart_query) or die(mysqli_error($con));
if(mysqli_num_rows($run_query)>0)
{
echo "<script>alert('You had already added this book in cart')</script>";
echo "<script>window.open('index.php','_self')</script>";
}
else
{
$insert_cart="insert into cart (cb_id, ip_add) values ('$cb_id','$ip')";
$run_insert= mysqli_query($con, $insert_cart) or die(mysql_error($con));
echo "<script>alert('Your book is added in the cart')</script>";
echo "<script>window.open('index.php','_self')</script>";
}
}
}
// IP功能:
function getIp()
{
$ip = $_SERVER['REMOTE_ADDR'];
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
return $ip;
}
//购物车表 cb_id(int 100) ip_add(varchar 255) 数量文字