![]() |
SQL to prevent duplicate Insertions - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: SQL to prevent duplicate Insertions (/showthread.php?tid=57454) |
SQL to prevent duplicate Insertions - El Forum - 03-15-2013 [eluser]Volkof[/eluser] Hi guys, Can anybody point out to me where is the error I am making here. Basically what I'm trying to do is to prevent duplicate Insertions for the 'review' column. For example, if someone types a review and presses the submit button twice, at least the model will check that the value is inserted on first button press and will not insert for second time. Code: INSERT INTO Review (review, userID, moduleID, recommendation) and I got shot back by this error Code: #1064 - 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 'WHERE NOT EXISTS but I couldnt figure out what is wrong. Can anyone enlighten me? Thank you SQL to prevent duplicate Insertions - El Forum - 03-15-2013 [eluser]TheFuzzy0ne[/eluser] Personally, I'd break that up into two queries. One to check if it's already in the database, and one to insert if it's not. There's the possibility of a race condition, but it's minimal. Alternatively, you could create a trigger on your database table. This post may help: http://stackoverflow.com/questions/1361340/how-to-insert-if-not-exists-in-mysql SQL to prevent duplicate Insertions - El Forum - 03-15-2013 [eluser]Otemu[/eluser] Hi, To add what TheFuzzy0ne just said, you should look at the codeigniter validation class "if someone types a review and presses the submit button" you could then check your database to see if a record exists. Example showing a callback: Code: <?php SQL to prevent duplicate Insertions - El Forum - 03-15-2013 [eluser]Volkof[/eluser] Thanks guys, I understand race condition is still a problem even if I break up the code, so I decided to gray out the Submit button after onclick. Just to share my solution VIEW Code: //Gray out submitBTN to prevent multiple clicks Controller Code: //Check for duplicated review Model Code: //Check if the review is in DB already |