SQL to prevent duplicate Insertions |
[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
[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/13613...s-in-mysql
[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
[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 |
Welcome Guest, Not a member yet? Register Sign In |