Problem with If statement

Hey,

I am trying to check to see if an email address exists in a database, and if it does INSERT a value…

I have this code:


						$email = Press::selectAllPressUsers();
						
						while($row = mysql_fetch_array($email)){
							if($_POST['email'] == $row['email']){ 
								$_SESSION['loggedin'] = true;
								$counter = 1;
							} else {
								$counter = 0;
							}
						}
					
						if($counter == 0){
								$message = Press::insertPressUser();
						} else {
								$message = Press::insertPressUserApproved();
						}

But when i type in an email address that already exists in the database it doesn’t do this: Press::insertPressUserApproved();…

Any ideas why?

Thanks

Your FOUND $counter value is being overwritten and once found you need to break out of your loop.

Try this:



$found = 'NOT FOUND'; // default if not found 

$i2 = array(1,2,3,4,5,6,7,8,9,10);	
foreach($i2 as $item):
	if ($item == 6)
	{
		$found = 'FOUND';
		break;
	}
endforeach;
echo '<br />$found => '. $item
die;


.

There’s nothing necessarily wrong with what you’re doing, it’s just a little bit inefficient (since it has to retrieve and loop through EVERYONE’s email address).

Hey,

I’ve changed my code to this:


						$email = Press::selectAllPressUsers();
						
						$counter = 0;
						
						while($row = mysql_fetch_array($email)){
							if($_POST['email'] == $row['email']){ 
								$counter = 1;
							}						
						}
					
						if($counter == 1){
								$message = Press::insertPressUserApproved();
						} else {
								$message = Press::insertPressUser();
						}

And it seem’s to be working fine, can you just look at this code and tell me if i’m doing anything wrong? If not, i will try your method :wink:

Thanks

It’s like hearing an echo…

If you want to check if a specific address exists, you can SELECT email FROM table WHERE email = ‘thevalueyouwanttocheck’ , and then use mysql_num_rows to determine it (if it’s 1, the email exists, otherwise it will be 0)
(NOTE: If you do this, sanitize the post variable before using it in a query!)

As far as your query, your problem is a question of rows

lets say the rows are:
me@com.com
test@find.me
jar@me.stop

and you’re looking for test@find.me. Your code does this:
Does me@com.com == test@find.me? No. Set Counter to 0.
Does test@find.me == test@find.me? Yes. Set Counter to 1.
Does jar@me.stop == test@find.me? No. Set Counter to 0.

At the end of it, Counter is 0. It will always be 0 unless the LAST row is the same as the one you’re looking for.

It seems you are trying to check the posted email address is already exists or not and do the action accordingly. Retrieving all records and loop through the whole rows to check is not the proper and good way. Do something like this:


$email = mysql_real_escape_string($_POST['email']);
$result = mysql_query("SELECT email FROM tblnema WHERE email='$email'") or die(mysql_error());
if(mysql_num_rows($result) >= 1){
    $message = Press::insertPressUserApproved();
}
else{
    $message = Press::insertPressUser();
}