Forum Moderators: coopster

Message Too Old, No Replies

LIMIT INSERT Statement?

         

HoboTraveler

7:53 am on May 11, 2007 (gmt 0)

10+ Year Member



Hi All,

I am looking for a way to ensure that data is inserted into the db once and not multiple times.

I created a simple form. When a user fills out the form and hits the submit, the data from the form gets inserted into a MySQL db.

Sometimes though, data may be inserted twice and this creates duplicates. Is there any possible way to ensure that data is inserted only once through an INSERT LIMIT?

Script: PHP
Database: MySQL

TIA

mikeyb

10:11 am on May 11, 2007 (gmt 0)

10+ Year Member



Hi,

Surely the only way to insert the same data twice is to submit the form twice with the same data?
Unless there is something wrong with your code of course...

But what you'd have to do is to check that the data does not exist in the dabase before doing the insert.
Of course, if the form is even one character different (eg a name field: "John Smith" vs "J Smith") then the data is not the same.

joelgreen

12:23 pm on May 11, 2007 (gmt 0)

10+ Year Member



You could add some hidden variable to the form, like num=123, different for each post.

Then when adding check the number. If number is the same - skip it. You could save a number in session for example.

HoboTraveler

1:00 pm on May 11, 2007 (gmt 0)

10+ Year Member



I guess an option would be to disable the submit button once it has been clicked.

coopster

1:10 pm on May 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



To ensure you don't insert duplicates in the database table, first your database table should be properly constructed. Either create your PRIMARY KEY and/or add UNIQUE keywords to the columns that should not or cannot be duplicated. You can then monitor for errors on INSERT operations and take appropriate action.

joelgreen

1:29 pm on May 11, 2007 (gmt 0)

10+ Year Member



You can then monitor for errors on INSERT operations and take appropriate action

This would not work on forum-like data.

eltreno

2:02 pm on May 11, 2007 (gmt 0)

10+ Year Member



In cases like these sometimes a user can hit refresh and data wil go in twice

you could hack it by setting a session with time of submission and don't let the borwser submit for another 2 mins or so!

jatar_k

4:46 pm on May 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> In cases like these sometimes a user can hit refresh and data wil go in twice

if you don't process on the form by submit self or some other foolishness then the user would have to go back and resub, making it more involved

you can tag it with an id to carry through the process, though the user can always go back through the process and resub unless you check for specific info as mentioned