1

Is there anyway of detecting a duplicate form submit with PHP?

I have a textfield which is used for typing a one-word phrase into it (eg. "Google"). When the user presses the submit button, the form containing that textfield gets submitted. After that - on the PHP processing page - some data gets selected from a database. If the submitted word doesn't exist, it gets inserted in to the db. But if it already exists, the existing one gets a vote up. This might be a little complicated, but I think it is understandable.

The only problem is that when a user submits something like this: "Google." it gets submitted, although there is already a record that contains "Google".

My question is simple: How can I check for duplicates?

[I already tried out the mysql function: LIKE, but it didn't really work, because when someone submitted something like this: "Goofy" it contained "Goo" in it which is already in the word "Google" -> which is already inserted. ]

So how could I solve this? Is there any good script for it?

Sam
  • 7,252
  • 16
  • 46
  • 65
Akos
  • 1,997
  • 6
  • 27
  • 40
  • 1
    You can do string comparisons using the equals sign if you're looking for exact matches. – hafichuk Nov 03 '11 at 14:31
  • 1
    Because you said `it gets a vote up` I made the conclusion it is about voting word, if I am not wrong then one thing to avoid here is have the same user vote twice or more. To manage this you have to associated each vote with a specific user, this means that if a word exists already and you have to make an up vote you must check if this user has voted previously. This can be done by having a user_id field as foreign key on the votes table, so check this field every time the user want to vote an existing word.The case that the word does not exist is already mentioned by other users here. – Melsi Nov 03 '11 at 14:43

4 Answers4

1

You could set the name column of the database as a unique index, which would mean any duplicate would not get written and you could check for that error in your insert SQL.

But it would be easier in the processing page to simply do a SELECT 'submittedword' FROM table-- if no result, then you know it's a new word, and you can do the insert. If it returns a result, then you do your increment function.

julio
  • 6,630
  • 15
  • 60
  • 82
1

This seems like a good application of INSERT ... ON DUPLICATE KEY UPDATE, where your key is a unique key, and your update is the number column.

nickb
  • 59,313
  • 13
  • 108
  • 143
1

This thread should help you.

It offers 2 solutions that should both work and depends on whether you want to modify your sql query or do the logic with php.

$result = mysql_query("update test set col='test' where col_id='1';");       
if (mysql_affected_rows()==0) {
    $result = mysql_query("insert into test (col_id, col) values ('1','test');");
}

You should also consider setting a unique index on the column that you don't want repeating.

It also offers: INSERT ON DUPLICATE KEY UPDATE.

Nightfirecat
  • 11,432
  • 6
  • 35
  • 51
Pixelmixer
  • 319
  • 1
  • 11
1

While I agree with @Dustin about using the INSERT ... ON DUPLICATE KEY UPDATE you may want to address this issue at the user input level with javascript or PHP during submission rather than having the SQL try and match the strings.

What are the restrictions of the input field currently. Would you expect users to be using punctuation, number, and other general non-alpha characters?

example of non-alpha: 1234567890-=()!@#$%^&*_+,./<>?;':"|\

If this is the case I would you a preg_replace or a regExp to remove any of these unwanted characters. Thus removing the problem at is sources.

Once you have validated/cleansed the input you can use a simple SQL insert like so:

INSERT INTO table (string, count) VALUES (1,2)
   ON DUPLICATE KEY UPDATE count=count+1;

Hope that helps a little.

Austin S.
  • 304
  • 1
  • 6
  • Good one... Just looking for this answer! It didn't tell everything, but I can handle it from now :) Accepted it. – Akos Nov 03 '11 at 15:28
  • If you neeed any more help just comment back here and I will do what I can! – Austin S. Nov 03 '11 at 20:22
  • also you need to make sure that whatever you are trying to check for duplicates is defined as a KEY in your table. – Austin S. Nov 03 '11 at 20:23