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";
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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";
}
?>
Hi,
what happens when you insert
at line 6 of youre code?
what happens when you insert
echo $email . " " .$row_Recordset1['customeremail'];
at line 6 of youre code?
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 "
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";
}
?>
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
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' ) ;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hmm. I am getting this
PHP Parse error: syntax error, unexpected $end
PHP Parse error: syntax error, unexpected $end
Are you closing the php ?>... in the example there was no ending php tag
ASKER
never mind, was just a missing } at the end
ASKER
That works great, thanks.
Open in new window
I am just curious as to why you don't check for the email with the query?