php - SQL = IF NOT EXISTS in a prepared statement? -


i'm trying check if record exists in table , if doesn't want execute insert... using prepared statement. can tell me what's wrong below? i've written code error checking , says query poo :) atrocious when comes sql , pretty programming related appreciate wisdom shared on dodgy looking quest... thanks!

        $mysqli = mysqli_connect($config['host'], $config['user'], $config['pass'], $config['db']);           $timestamp = time();            $stmt = $mysqli->prepare("if not exists (select id course_licence_cart userid = ? , courseid = ? , lmsid = ?) begin insert course_licence_cart (lmsid, userid, courseid, assigned_by, assigned_on) values (?, ?, ?, ?, ?) end");          foreach($_post['assignto'] $assignto){             $stmt->bind_param('iiiiiiii', $assignto, $_post['course'], $core['id'], $core['id'], $assignto, $_post['course'], $userinfo['id'], $timestamp);             $stmt->execute();         } 

fyi: takes place after form submission, i've checked of variables , they're good, , process works (i have working without if not exists), it's new query type mixed prepared statements has totally thrown me off.

-- update --

i ran following directly:

if not exists (select `id` `course_licence_cart` `userid` = '175' , `courseid` = '1' , `lmsid` = '1') begin insert `course_licence_cart` (`lmsid`, `userid`, `courseid`, `assigned_by`) values ('1', '175', '1', '175') end 

in error:

you have error in sql syntax; check manual corresponds mysql server version right syntax use near 'if not exists (select `id` `course_licence_cart` `userid` = '175' and' @ line 1 

first of all root of error has nothing prepared statements. it's can't use if, begin ... end blocks , other constructs out of scope of stored routine (procedure, function, trigger, event).

to prevent duplicates can leverage insert ignore

$stmt = $mysqli->prepare("insert ignore course_licence_cart (lmsid, userid, courseid, assigned_by, assigned_on) values (?, ?, ?, ?, ?)");  foreach($_post['assignto'] $assignto){     $stmt->bind_param('iiiiiiii', $assignto, $_post['course'], $core['id'], $core['id'], $assignto, $_post['course'], $userinfo['id'], $timestamp);     $stmt->execute(); } 

in order work have have unique constraint defined.

create unique index index_name      on course_licence_cart (userid, courseid, lmsid); 

here sqlfiddle demo


now code (after correcting syntax) could've worked in stored procedure so

delimiter // create procedure add_to_cart(in _lmsid int, in _userid int, _courseid int, in _assigned_by int, in _assigned_on datetime) begin     if not exists (select *                      course_licence_cart                      userid = _userid                        , courseid = _courseid                       , lmsid = _lmsid)         insert course_licence_cart (lmsid, userid, courseid, assigned_by, assigned_on)          values (_lmsid, _userid, _courseid, _assigned_by, _assigned_on);     end if; end// delimiter ; 

here sqlfiddle demo

in case php code like

$stmt = $mysqli->prepare("call add_to_cart (?, ?, ?, ?, ?)");  foreach($_post['assignto'] $assignto){     $stmt->bind_param('iiiiiiii', $assignto, $_post['course'], $core['id'], $core['id'], $assignto, $_post['course'], $userinfo['id'], $timestamp);     $stmt->execute(); } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -