php - What is wrong about this SQL statement? -


here containing php file:

<?php session_start(); include("../includes/dbcxnfunction.inc");  $billid = $_post['billid']; $userid = $_session['userid'];  $query = "if exists ( select * favoritebills userid = '$userid' , billid = '$billid' )               delete favoritebills userid = '$userid' , billid = '$billid'                    else               insert favoritebills (userid,billid) values($userid,$billid) ";  $result = mysqli_query(dbcxn('bill'),$query)               or exit("couldn't execute query favorites");  if($result)     {     $request = "true";     } else      {         $request = "false";     }  echo $request;  ?> 

the query is:

"if exists ( select * favoritebills userid = '$userid' , billid = '$billid' )           delete favoritebills userid = '$userid' , billid = '$billid'                else           insert favoritebills (userid,billid) values($userid,$billid) "; 

userid , billid both integers.

this network tab:

enter image description here

in .js file have console.log(response);

the console logging "couldn't execute query favorites"

you can't use if statement in query, can used in stored procedure. want, need 2 separate queries. try this:

$del_query = "delete favoritebills userid = '$userid' , billid = '$billid'"; $ins_query = "insert 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) { // row didn't exist, add   $res = mysqli_query(dbcxn('bill'),$ins_query)     or die ("couldn't execute insert query: " . dbxcn('bill')->error); }  $result = "true"; // no need test false, since die above in case 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -