1

I'm trying to check if a record already exists in my table and if it doesn't I want to execute an insert... using a prepared statement. Can anyone tell me what's wrong below? I've written the code with error checking and it basically says the query is poo :) I am atrocious when it comes to SQL and pretty much anything programming related so I really do appreciate any wisdom that shared on this 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 FROM course_licence_cart WHERE userid = ? AND courseid = ? AND lmsid = ?) BEGIN INSERT INTO course_licence_cart (lmsid, userid, courseid, assigned_by, assigned_on) VALUES (?, ?, ?, ?, ?) END");

        foreach($_POST['assignTo'] as $assignTo){
            $stmt->bind_param('iiiiiiii', $assignTo, $_POST['course'], $core['id'], $core['id'], $assignTo, $_POST['course'], $userInfo['id'], $timestamp);
            $stmt->execute();
        }

FYI: This takes place after a form submission, I've checked ALL of the variables and they're all good, and the process itself works perfectly (I have it working without the IF NOT EXISTS), it's just this new query type mixed with prepared statements that has totally thrown me off.

-- UPDATE --

I ran the following directly:

IF NOT EXISTS (SELECT `id` FROM `course_licence_cart` WHERE `userid` = '175' AND `courseid` = '1' AND `lmsid` = '1') BEGIN INSERT INTO `course_licence_cart` (`lmsid`, `userid`, `courseid`, `assigned_by`) VALUES ('1', '175', '1', '175') END

In which I get the error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF NOT EXISTS (SELECT `id` FROM `course_licence_cart` WHERE `userid` = '175' AND' at line 1
6
  • have you tried the statement directly?
    – Hogan
    Apr 13, 2014 at 2:13
  • possible duplicate of How to 'insert if not exists' in MySQL?
    – Hogan
    Apr 13, 2014 at 2:14
  • There is even a PHP example as one of the answers, which this question was not tagged
    – Hogan
    Apr 13, 2014 at 2:15
  • possible duplicate of stackoverflow.com/questions/7113680/mysql-if-not-exist
    – Nathan
    Apr 13, 2014 at 2:32
  • 2
    There is no such test as 'IF NOT EXISTS (...' for SQL. see 'google'. Which makes no sense for 'sets of data' anyway. You know your checking one row. Sadly, the database engine doesn't. Reading your question - there are NO NEW QUERY TYPES by using 'prepared queries'! It is the exact same queries just a different and safer way of calling them! Apr 13, 2014 at 3:13

3 Answers 3

2

First of all the root of the error has nothing to do with prepared statements. It's just you can't use IF, BEGIN ... END blocks and other constructs out of the scope of a stored routine (procedure, function, trigger, event).

To prevent duplicates you can leverage INSERT IGNORE like so

$stmt = $mysqli->prepare("INSERT IGNORE INTO course_licence_cart (lmsid, userid, courseid, assigned_by, assigned_on) VALUES (?, ?, ?, ?, ?)");

foreach($_POST['assignTo'] as $assignTo){
    $stmt->bind_param('iiiiiiii', $assignTo, $_POST['course'], $core['id'], $core['id'], $assignTo, $_POST['course'], $userInfo['id'], $timestamp);
    $stmt->execute();
}

In order for this to work you have to have a unique constraint defined.

CREATE UNIQUE INDEX index_name 
    ON course_licence_cart (userid, courseid, lmsid);

Here is SQLFiddle demo


Now your code (after correcting syntax) could've worked in a stored procedure like 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 *
                     FROM course_licence_cart 
                    WHERE userid = _userid 
                      AND courseid = _courseid
                      AND lmsid = _lmsid) THEN
        INSERT INTO course_licence_cart (lmsid, userid, courseid, assigned_by, assigned_on) 
        VALUES (_lmsid, _userid, _courseid, _assigned_by, _assigned_on);
    END IF;
END//
DELIMITER ;

Here is SQLFiddle demo

In this case php code would look like

$stmt = $mysqli->prepare("CALL add_to_cart (?, ?, ?, ?, ?)");

foreach($_POST['assignTo'] as $assignTo){
    $stmt->bind_param('iiiiiiii', $assignTo, $_POST['course'], $core['id'], $core['id'], $assignTo, $_POST['course'], $userInfo['id'], $timestamp);
    $stmt->execute();
}
1
  • What sort of transaction scope does he need to prevent insertion of duplicates in the sp version, assuming interleaved threads? Apr 13, 2014 at 3:29
0

Since you're coding in PHP, one thing to consider is doing a sql query to SELECT from that table, if nothing is returned, run an insert query.

I'm sure it can be doing through SQL, but I personally don't know how.

Good luck, sorry if this doesn't help.

1
  • ... You could give an example, otherwise this should be a comment. Note that the scheme suggested (and that he's currently experimenting with in the db) is not threadsafe, and would probably require locking the entire table (assuming no UNIQUE key could be used) - which would kill concurrency. Apr 13, 2014 at 3:26
0

in MYSQL there are 4 types of inserts available. 1)INSERT 2)INSERT IGNORE 3)INSERT ON DUPLICATE KEY 4)REPLACE

Please explore on them. As far as I can understand your specific case can be handled by INSERT IGNORE command.

Note : I am assuming here that Mysqli is same as MYSQL

Update: now I know that mysqli is interface to MYSQL.But the concept of insert ignore will still be same. example to understand use of INSERT IGNORE consider below table.

CREATE TABLE person_tbl ( first_name CHAR(20) NOT NULL, last_name CHAR(20) NOT NULL, sex CHAR(10), PRIMARY KEY (last_name, first_name) );

here firstname&lastname forms primary key

now we run query

mysql> INSERT INTO person_tbl VALUES( 'Jay', 'Thomas');

it will add one row in table

if we will run the above sql it will fail with duplicate record exception.to prevent this exception we have 2 options

1)check if record already exists,if not then insert record.... we have to fire select and then insert query.

2) fire INSERT IGNORE SQL.. it will check the if record exist in table if exist then it will not insert record ,if not then inserts record.

for e.g if we run query

mysql> INSERT IGNORE INTO person_tbl VALUES( 'Jay', 'Thomas');

this sql will not insert any record ..as record already exists

but

mysql> INSERT IGNORE INTO person_tbl VALUES( 'Vijay', 'Thomas');

it will insert one record in table.

1
  • This should really be a comment. If you think that INSERT IGNORE is what he needs, give an example in use. MySQL is a particular "flavor" (implementation) of an RDBMS, MySQLi is a set of interface methods in PHP (that is, no, they're not the same thing - and I'm not versed enough to know if MySQLi only interfaces with MySQL, or any database). Apr 13, 2014 at 3:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.