Check if email exists in registration

Post here is you are having problems with any of the tutorials.
Post Reply
shaunthomson
Posts: 19
Joined: Mon Nov 28, 2011 11:53 am

Check if email exists in registration

Post 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
Last edited by jacek on Tue Nov 29, 2011 5:45 pm, edited 1 time in total.
Reason: code tags...
salrathor1
Posts: 26
Joined: Tue Jun 21, 2011 2:23 am

Re: Check if email exists in registration

Post 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
shaunthomson
Posts: 19
Joined: Mon Nov 28, 2011 11:53 am

Re: Check if email exists in registration

Post by shaunthomson »

That fixed it up - thanks Sal!!!
Post Reply