Jump to content

check for duplicates


fife

Recommended Posts

Hello im trying to write a simple script so that before my website inserts an account into the database it checks to see if that email already exists.  Heres what i got but it does not work.

 

//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);
$GotEmail = mysql_num_rows($EmailResult);	   

if($GotEmail != 0) {
    $emailDup= "This email account has already been registered";
}
else {
//run the rest of my scripts 

 

later down the page I have this to echo the message

 <? if($GotEmail)==1 echo $emailDup; ?>

Link to comment
https://forums.phpfreaks.com/topic/210856-check-for-duplicates/
Share on other sites

You get that error when you have a syntax error in your mysql query. How are you setting the variable $email?

 

Change the line

<?php
$EmailQuery = mysql_query($CheckEmail);	
?>

to

<?php
$EmailQuery = mysql_query($CheckEmail) or die("Problem with the query: $EmailQuery<br>" . mysql_error());
?>

to see the error.

 

Ken

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 [email protected]"; 
     }
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);
$GotEmail = mysql_num_rows($EmailResult);	   

if($GotEmail != 0) {
    $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();
}    
}    
}

 

then in the middle of the page i have my echo $emailDup

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.