function cart() {
if(isset($_GET['add_cart'])){
global $connect;
$ip = getIp();
$pro_id = $_GET['add_cart'];
$check_pro = "SELECT * FROM cart WHERE ip_add = '$ip' AND p_id = '$pro_id'" ;
$run_check = mysqli_query($connect, $check_pro);
if(mysqli_num_rows($run_check)>0){
echo "do a thing";
}else {
$insert_pro = "insert into cart (p_id,ip_add) values ('$pro_id','$ip')";
$run_pro = mysqli_query($connect, $insert_pro);
if($run_pro){
echo "<script>window.open('index.php','_self')</script>";
}
}
}
Im most likely missing something so obvious so thank you in advance.
The qty
column doesn't have a default value, and you're not providing a value for it when you insert the new row. This will cause an error. You need to specify a value, eg 0
.
Also, since p_id
is an INT
column, you don't need to put the value in quotes.
$insert_pro = "insert into cart (p_id,ip_add, qty) values ($pro_id,'$ip', 0)";
But better would be to use a prepared statement to protect against SQL injection.
$insert_pro = $connect->prepare("insert into cart (p_id,ip_add, qty) values (?, ?, 0)");
$insert_pro->bind_param("is", $pro_id, $ip);
$run_pro = $insert_pro->execute();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.