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
Share on other sites

basically I get this error

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/sites/yourarena.co.uk/public_html/member_join.php on line 25

 

which which is this line

 

$GotEmail = mysql_num_rows($EmailResult);

Link to comment
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

Link to comment
Share on other sites

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);
$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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.