loop through records to check if email exists

jonofat
jonofat used Ask the Experts™
on
I am trying to loop through records once a user types in their email address to see if it exists but I am not quite getting it..

 
$email = $_POST['email'];
   $query_Recordset1 = "SELECT customeremail FROM customers";
   $Recordset1 = mysql_query($query_Recordset1, $evs) or die(mysql_error());
   while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)){;
   $totalRows_Recordset1 = mysql_num_rows($Recordset1);
   if ( $email == $row_Recordset1['customeremail'] ) {
	echo "That email exists";
} else {
	echo "That email doesn't exist";
}
   }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Greg AlexanderLead Developer

Commented:
You have an extra semi-colon, everything else looks good, try this

<?
	$email = $_POST['email'];
	$query_Recordset1 = "SELECT customeremail FROM customers";
	$Recordset1 = mysql_query($query_Recordset1, $evs) or die(mysql_error());
	
	while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)){
		$totalRows_Recordset1 = mysql_num_rows($Recordset1);
		
		if ( $email == $row_Recordset1['customeremail'] ) {
			echo "That email exists";
		} else {
			echo "That email doesn't exist";
		}
	}
?>

Open in new window


I am just curious as to why you don't check for the email with the query?
Marco GasiFreelancer
Top Expert 2010
Commented:
Try this:

$email = $_POST['email'];
$query_Recordset1 = "SELECT customeremail FROM customers <b>WHERE cusomeremail='$email'</b>";
$Recordset1 = mysql_query($query_Recordset1, $evs) or die(mysql_error());
if (mysql_num_rows($Recordset1) > 0){
  echo "That email exists";
} else {
  echo "That email doesn't exist";
}

Cheers
Greg AlexanderLead Developer

Commented:
Here is what I was talking about:

<?
	$email = $_POST['email'];
	$query_Recordset1 = "SELECT customeremail FROM customers WHERE customeremail = '".$email."'";
	$Recordset1 = mysql_query($query_Recordset1, $evs) or die(mysql_error());
	
	if(mysql_num_rows($Recordset1)){
		echo "That email exists";
	}else{
		echo "That email doesn't exist";
	}
?>

Open in new window

Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Hi,

what happens when you insert

echo $email . "   " .$row_Recordset1['customeremail'];

Open in new window


at line 6 of youre code?

Author

Commented:
Thanks, but it gives me double answers.

If the email does exist it says "That email existsThat email doesn't exist "

and if the email doesn't exist it says "That email doesn't existThat email doesn't exist "
Greg AlexanderLead Developer

Commented:
Try my second solution
<?
        $email = $_POST['email'];
        $query_Recordset1 = "SELECT customeremail FROM customers WHERE customeremail = '".$email."'";
        $Recordset1 = mysql_query($query_Recordset1, $evs) or die(mysql_error());
        
        if(mysql_num_rows($Recordset1)){
                echo "That email exists";
        }else{
                echo "That email doesn't exist";
        }
?>

Open in new window

Author

Commented:
Oh wait, new answers... Let me test..
Marco GasiFreelancer
Top Expert 2010

Commented:
@galexander07: your second solution was my first solution, it isn't? :-)
Greg AlexanderLead Developer

Commented:
Being as they were posted on the same minute, It was just a coincidence :) and they are not exactly the same... but both would work

Author

Commented:
MargusG, yours worked. However, is it possible to make it redirect? It is meant to insert record if the email exists and not insert/redirect if it doesn't exist.

  $email = $_POST['email'];
$query_Recordset1 = "SELECT customeremail FROM customers WHERE customeremail='$email'";
$Recordset1 = mysql_query($query_Recordset1, $evs) or die(mysql_error());
if (mysql_num_rows($Recordset1) > 0){

$insertSQL = sprintf("INSERT INTO tokens (email, token) VALUES (%s, %s)",
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['token'], "text"));

  mysql_select_db($database_test, $test);
  $Result1 = mysql_query($insertSQL, $test) or die(mysql_error());
}
} else {
   header( 'Location: doesnt_exist.php' ) ;
}

Open in new window

Lead Developer
Commented:
You can use the attached to redirect... you had one to many ending brackets before the else
<?
$email = $_POST['email'];
$query_Recordset1 = "SELECT customeremail FROM customers WHERE customeremail='$email'";
$Recordset1 = mysql_query($query_Recordset1, $evs) or die(mysql_error());
if (mysql_num_rows($Recordset1) > 0){

$insertSQL = sprintf("INSERT INTO tokens (email, token) VALUES (%s, %s)",
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['token'], "text"));

  mysql_select_db($database_test, $test);
  $Result1 = mysql_query($insertSQL, $test) or die(mysql_error());
} else {
   header( 'Location: doesnt_exist.php' ) ;
}

Open in new window

Author

Commented:
Hmm. I am getting this

PHP Parse error: syntax error, unexpected $end
Greg AlexanderLead Developer

Commented:
Are you closing the php ?>... in the example there was no ending php tag

Author

Commented:
never mind, was just a missing } at the end

Author

Commented:
That works great, thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial