Page 1 of 1

Check if email exists in registration

Posted: Tue Nov 29, 2011 8:43 am
by shaunthomson
Hi

I have the login/registration tut up and running now, complete with your email activation. THANKS!!!

As well as checking if the username already exists, I'd like to check if the email exists in the database too, but can't find where I'm going wrong.

In user.inc.php, I added the following function:
function email_exists($email) {	
	
	$email = mysql_real_escape_string($email);
	$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_email` = '{$email}'");
	return (mysql_result($total, 0) === '1') ? true : false;	
}
>>ps - the ' and " in '{$email}'" are correct, they just look strange<<

...and in register.php, I put:
if(email_exists($_POST['email'])) {
			
			$errors[] = 'The email you have chosen is already registered.  Only one email address per registration is allowed.';			
		}

...this doesn't show up the email error, even when the email address is definitely already used. The other errors show up fine. I've tried echoing the email in the email_exists function, and it definitely displays, so it's getting to the function.

Anything I've overlooked here?

Thanks guys

Shaun

Re: Check if email exists in registration

Posted: Tue Nov 29, 2011 3:58 pm
by salrathor1
Hey,

Maybe someone can expand because i'm pretty new to PHP, but you're using a tripple === whereas Jacek uses a double == in his tutorials. Maybe that's why it's failing?

=== checks if the value and type are the same, whereas == checks if the values are the same without necessarily being the same data type.

For example,
if (1 == '1') - This would be true because you're checking the values and not the data types.
whereas
if (1 === '1') would be false, because you're comparing the number 1 to the string '1'.

Let me know if this works.
Sal

Re: Check if email exists in registration

Posted: Wed Nov 30, 2011 1:20 am
by shaunthomson
That fixed it up - thanks Sal!!!