You can’t use IF
as a statement in a query, it can only be used in a stored procedure. To do what you want, you need two separate queries. Try this:
$del_query = "DELETE FROM favoritebills WHERE userid = '$userid' and billid = '$billid'";
$ins_query = "INSERT INTO favoritebills (userid,billid) VALUES($userid,$billid) ";
$res = mysqli_query(dbcxn('bill'),$del_query)
or die ("Couldn't execute DELETE query: " . dbxcn('bill')->error);
if (dbcxn('bill')->affected_rows == 0) { // The row didn't exist, so add it
$res = mysqli_query(dbcxn('bill'),$ins_query)
or die ("Couldn't execute INSERT query: " . dbxcn('bill')->error);
}
$result = "true"; // No need to test for false, since we die above in that case
11
solved What is wrong about this SQL statement? [closed]