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

PHP

Avatar of undefined
Last Comment
jonofat
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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
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
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
jonofat

ASKER

Oh wait, new answers... Let me test..
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

@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
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
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of jonofat
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
jonofat

ASKER

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

ASKER

That works great, thanks.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo