I can't figure out the way out of my problem, Is there a better way to do this?.
It's supposed to check if the data exist and if it does, update the last table quantity with the sum of it and the new quantity. And if it doesn't exist create the table. But I can't get it to work, the table just updates.(there's no data in the database)
Here is my php:
$SESSID = session_id();
//(session_id,bid,date_added,qty)
$q = "SELECT quantity FROM cart WHERE session_id = '$SESSID' AND item_id = '$itemID'";
$r = mysqli_query($dbc, $q);
$check = mysqli_fetch_assoc($r);
if($check=='' && $r==FALSE){
$addtocart = "INSERT INTO cart (item_id, quantity, date_added, session_id) VALUES ('$itemID', '$qty', '$datetime', '$SESSID')";
echo 'create';
}else{
$total = $r + $qty;
echo $total;
$addtocart = "UPDATE cart SET quantity = '$total' WHERE session_id = '$SESSID' AND item_id = '$itemID'";
echo 'update';
}
$r = mysqli_query($dbc, $addtocart);
//go to showcart
/*header("location:../tienda?cart");*/
exit;
}