Link to home
Start Free TrialLog in
Avatar of jonofat
jonofat

asked on

loop through records to check if email exists

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

Avatar of Greg Alexander
Greg Alexander
Flag of United States of America image

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?
SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of dr_Pitter
dr_Pitter

Hi,

what happens when you insert

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

Open in new window


at line 6 of youre code?
Avatar of jonofat

ASKER

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 "
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

Avatar of jonofat

ASKER

Oh wait, new answers... Let me test..
@galexander07: your second solution was my first solution, it isn't? :-)
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
Avatar of jonofat

ASKER

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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jonofat

ASKER

Hmm. I am getting this

PHP Parse error: syntax error, unexpected $end
Are you closing the php ?>... in the example there was no ending php tag
Avatar of jonofat

ASKER

never mind, was just a missing } at the end
Avatar of jonofat

ASKER

That works great, thanks.