Page 1 of 1

Check for duplicate before Insert!

Posted: Mon Aug 16, 2010 9:14 am
by Ring Master
Hi. I have built a form that inserts a new user into the database. It works great and so do all my error checks i run. Now I noticed that if there is already an account found in my database with an email that has already been entered before I get a sql error. This will not look good to my clients so Im trying to write something that checks the database first. The code I wrote errors. I believe the error to be saying its not finding any records in my database. I have put notes in my script to you can find what I mean. Here is the error i get;

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/sites/member_join.php on line 73
Duplicate entry 'email@email.com' for key 2

and the code.

Code: Select all


session_start();  
$validation_id = strval(time());

if(isset($_POST['submit'])){    //Process data for validation    
$first_name    = trim($_POST['first_name']);    
$last_name     = trim($_POST['last_name']);    
$DOB           = trim($_POST['DOB']);    
$sex           = trim($_POST['sex']);    
$email         = trim($_POST['email']);    
$username      = trim($_POST['username']);    
$password      = trim($_POST['password']);     
$agree         = trim($_POST['agreed']);    
$creation_date = trim($_POST['creation_date']);    
$usertype     = trim($_POST['usertype']);    
$access_level  = trim($_POST['today_is']);    
$validation    = trim($_POST['to']);    
$jobs     = trim($_POST['jobs']); 
   
//Perform validations    
$errors = array();    
if(empty($first_name))    {        
$errors[] = "Please enter a first name";    
}
if(empty($last_name))    { 
$errors[] = "Please enter a surname";    
}    
if(empty($DOB))    {        
$errors[] = "Please enter your date of birth.";    
}    
else if(!(preg_match("/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/", $DOB)))    {        
$errors[] = "Please enter your birthday in the format dd/mm/yyyy";    
}       
if(empty($email))    {        
$errors[] = "Please enter a correct email.";    
}  
elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){ 
$errors[] = "Please enter a valid email joe_blogs@example.co.uk"; 
     }
if(empty($username))    {        
$errors[] = "Please enter a username.";    
}       
if(strlen($password)<6)    {        
$errors[] = "Please enter a password greater than 6 characters long.";    
}   
//Check if there were errors
if(count($errors)===0)    {        
	//Prepare data for db insertion        
	$first_name    = mysql_real_escape_string($first_name);        
	$last_name     = mysql_real_escape_string($last_name);        
	$DOB           = mysql_real_escape_string($DOB);        
	$sex           = mysql_real_escape_string($sex);        
	$email         = mysql_real_escape_string($email);        
	$username      = mysql_real_escape_string($username);        
	$password      = md5($password);         
	$agree         = mysql_real_escape_string($agree);        
	$creation_date = mysql_real_escape_string($creation_date);        
	$usertype     = mysql_real_escape_string($usertype);        
	$access_level  = mysql_real_escape_string($access_level);        
	$validation    = mysql_real_escape_string($validation);        
	$jobs     = mysql_real_escape_string($jobs);

			//switch date around for database insertion	
	$date_parts = explode('/', $DOB);        
	$DOB_new = "{$date_parts[2]}/{$date_parts[1]}/{$date_parts[0]}"; 

//script for checking if the email account already exists
$CheckEmail = "SELECT * FROM Members WHERE email='".$email."'";
$EmailQuery = mysql_query($CheckEmail);
$Emailresult = mysql_fetch_array($EmailQuery);


if (mysql_num_rows($EmailResult)=='1')  
  {$emailDup = "This email account has already been registered";}
else
{ 
//run the rest of my scripts
//insert
	$query = "INSERT INTO Members                      
	(`first_name`, `last_name`, `DOB`, `sex`, `email`, `username`, `password`, `agree`, `creation_date`, `usertype`, `access_level`, `validationID`,`jobs`)
	VALUES                    
	('{$first_name}', '{$last_name}', '{$DOB_new}', '{$sex}', '{$email}', '{$username}', '{$password}', '{$agree}', '{$creation_date}', '{$usertype}', '{$access_level}', '{$validation}', '{$jobs}')";
			
	$result= mysql_query($query) or die(mysql_error()); 
		   //send validation to the thankyou page
	$url = "thankyou.php?to={$validation}";        
	header("Location: {$url}"); 
		 exit();
}    
}    
}

Re: Check for duplicate before Insert!

Posted: Mon Aug 16, 2010 12:56 pm
by Gargoyle

Code: Select all

mysql_num_rows($EmailQuery)

Re: Check for duplicate before Insert!

Posted: Mon Aug 16, 2010 3:09 pm
by liamallan
try changing:

Code: Select all

if (mysql_num_rows($EmailResult)=='1') 
to:

Code: Select all

if ($email == $EmailResult['email'])