1

Possible Duplicate:
How to ‘insert if not exists’ in MySQL?

I have a few inserts and I only want them to actually insert if there isn't already the same information in there.

I only want the insert to run using WHERE NOT EXISTS, would anybody be able to help me adding that to my queries?

Queries:

$db->Query("INSERT INTO `surfed` (site) VALUES('".$id."')");

Thanks so much!

2
  • I dare say this has been asked before. Commented Oct 29, 2012 at 17:01
  • I read them but it wouldn't work for my query. Commented Oct 29, 2012 at 17:07

3 Answers 3

4

Use the IGNOREkeyword

INSERT IGNORE INTO surfed (user, site) VALUES('".$data['id']."', '".$id."')

If you have a unique key constraint on the user-site combination then it would normally generate an error on duplicates and abort the insertion. Using ignore in the insert statement will ignore the error and just not insert anything.

If you use the IGNORE keyword, errors that occur while executing the INSERT statement are treated as warnings instead. ... With IGNORE, the row still is not inserted, but no error is issued.

See here for more infos about it

SQLFiddle example

Sign up to request clarification or add additional context in comments.

5 Comments

So by using the code you supplied it will only submit in to the database if it doesn't already exist and not produce any errors?
Yes. If you have a unique key constraint on the user-site combination.
This doesn't seem to be working, I have removed the INSERT in to user because I only want site to be submitted. So now it currently looks like: $db->Query("INSERT IGNORE INTO surfed (site) VALUES('".$id."')"); + $db->Query("INSERT IGNORE INTO surfed (site) VALUES('".$sit['id']."')"); Basically lots of people view lots of different sites but I only want a certain site to be submitted once in to the site column.
Do you have a unique key on site? If so, then the insert should fail and not return an error.
0

You should be able to go with mysql_num_rows. If that returns a value of 0, then there is nothing in your table matching what you want to insert. (WHERE NOT EXISTS). So make something like "if mysql_num_rows = 0 then insert else do nothing".

Comments

0

First your two columns need to be a combined key, then you can use INSERT IGNORE as outlined by juergen d or use

ON DUPLICATE KEY UPDATE user=user

See similar question here: INSERT ... ON DUPLICATE KEY (do nothing)

For the revised question of only having a site column, make sure the site column is a unique key or primary key and then use

$db->Query("INSERT INTO `surfed` (site) VALUES('".$id."') ON DUPLICATE KEY UPDATE site=site");

1 Comment

Hey, thanks for the replies but I have removed the users column, I only want the site to be submitted. If the site is already submitted then I don't want it to be submitted again. Would you be able to show me how to do this?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.