mysql if exists giving error wrong syntax? -


i trying use if exists in mysql keep getting syntax errors , have researched correct syntax isnt working...

what need is: if query exists update else insert new...

$querystring =  "if exists (select * $oncall_table uid='$contextuser' , submitid='$submitid' , submitstatus=3) update $oncall_table set uid='$contextuser', start_time='$onstr', end_time='$offstr', amount='$amount' else insert $oncall_table (uid, start_time, end_time, amount) values ('$contextuser','$onstr', '$offstr', '$amount') end if"; 

error message: can't perform query: have error in sql syntax; check manual corresponds mysql server version right syntax use near 'if exists (select * timesheet_oncall uid='admin' , submitid='136545' @ line 1

replace into need. http://dev.mysql.com/doc/refman/5.0/en/replace.html

replace works insert, except if old row in table has same value new row primary key or unique index, old row deleted before new row inserted.

in case

replace  $oncall_table (uid, start_time, end_time, amount)  values ('$contextuser','$onstr', '$offstr', '$amount') uid='$contextuser'; 

assuming uid primary key or unique key

note: since code in question contains sql injection flaws recommend read article. http://php.net/manual/en/security.database.sql-injection.php


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -