我正在开发一个电子商务网站。其中,我添加了购物车的代码。但是,它无法将数据存储在mysql的cart表中。
以下是代码:
$host = "localhost";
$user = "root";
$password = "";
$database = "puri_database";
$db = mysqli_connect($host, $user, $password, $database);
//Function for getting user ip address
function getRealIpAddr() {
if (!empty($_SERVER['HTTP_CLIENT_IP']))
//check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty ($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
function cart() {
global $db;
if(isset($_GET['add_cart'])){
$p_id = $_GET['add_cart'];
$ip_add = getRealIpAddr();
$check_pro = "select * from cart where p_id='$p_id' AND ip_add='$ip_add'";
$run_check = mysqli_query($db,$check_pro);
if(mysqli_num_rows($run_check)>0){
echo "Product is Already Exist";
}
else{
$q = "insert into cart (p_id,ip_add) values ('$p_id','$ip_add')";
$run_q = mysqli_query($db,$q);
echo "Product is added to cart successfully";
}
}
}
你能帮我们解决这个问题吗?