Jump to content


Photo

Preventing Duplicate Entries in MySQL


  • Please log in to reply
5 replies to this topic

#1 jawinn

jawinn
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 03 October 2006 - 07:04 PM

I have a web form that dumps form data into a DB. One of the fields is for email address. I would really like the form to kick back an error message if an email address is used that is already in the DB. My DB only has one table. What would the code be for that?

Thanks in advance,
J

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 03 October 2006 - 08:52 PM

There are lots of ways to handle this, depending on whether you can't have duplicates ever, or just do want to add new ones.  You can simply query the table before and see if another matching entry is found, or you can strictly enforce it with an index.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 jawinn

jawinn
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 03 October 2006 - 09:04 PM

This is an empty DB.  I will be empting it weekly and eventually daily.  I want to prevent ANY duplicates for a specific column.

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 03 October 2006 - 09:28 PM

Then go with a UNIQUE index.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 jawinn

jawinn
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 03 October 2006 - 09:30 PM

Then go with a UNIQUE index.


How do I add that to my script?

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 04 October 2006 - 07:36 PM

You have to modify your script to see if your INSERT failed.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users