Checking For Duplicate Entries In Mysql database

steves

Regular Member
Joined
Jun 6, 2007
Messages
220
Reaction score
83
My landing page is capturing all of the data and I am using php to make sure everything is filled out the way I want to. Problem I am having now is some people are submitting more then one time. I would like to be able to check the database for there email address first, if it is not there then it will go on to verify that the fields are filled in properly and store it int he db. My code looks like this so far...

Code:
$search_email = "SELECT email FROM db where email = $email";
if (mysql_query($search_email)){
echo 'That email address is already in our system';}


else{

verification code goes here and then submits to the db

Right now it just skips past checking the address and submits it. Anyone able to give me a hand with this headache?
 
I figured it out. This is the code I used to check for duplicate entries.

Code:
$search_email = "SELECT email FROM db where email = '$email'";
$result = mysql_query($search_email);  
if (mysql_num_rows($result)>0)
{
echo 'That email address is already in our system';
}  
 
else {

rest of form verification code goes here
 
NO no no! You're opening yourself up to SQL injection by just inserting $email in there. Make sure you filter your input before submitting database queries!
 
mysql_real_escape_string()

Code:
$email = mysql_real_escape_string($email);
$search_email = "SELECT email FROM db where email = '$email'";
$result = mysql_query($search_email);  
if (mysql_num_rows($result)>0)
{
echo 'That email address is already in our system';
}  
 
else {

rest of form verification code goes here
 
Last edited:
why not an unique index in the email field? It would be one query less

The only thing is to handle the error properly
 
Back
Top