Jump to content

Check For Existing Email Address Fails


son

Recommended Posts

I have a basic registration system which should bring up the error message 'That email address has already been registered.' when someone tries to register with already registered email address. Although I do my check with $emailQuery when entering an existing email address is always brings up the error message for the failed insert query ($query). This query should only be run when the email address is not available. I echoed $emailQuery and copied it into phpMyAdmin. The query correctly brings back the relevant user_id. I am lost with this one... Where am I going wrong?

	if ($first_name && $last_name && $email) // if everything filled out ok	{ 	// make sure the email address is available	$emailQuery = "SELECT user_id FROM users WHERE email='$email'";			$emailResult = mysqli_query ($dbc, $emailQuery) or trigger_error("Query: $emailQuery\n<br />MySQL Error: " . mysql_error());		if (mysqli_num_rows($dbc, $emailResult) == 1) // email address is not available		{ 		echo '<p>That email address has already been registered. </p>'; 				} 		else // the email address is available		{ 		// add the user		$query = "INSERT INTO users (email, first_name, last_name) VALUES ('$email', '$first_name', '$last_name')";				$result = mysqli_query ($dbc, $query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());			if (mysqli_affected_rows($dbc) == 1) // If it ran ok			{ 			// send the email			$body = "Thank you for registering with us.\n";			mail($_POST['email'], 'Registration Confirmation', $body, 'From: info@testing.co.uk');			// finish the page       		header ("Location: register-success.php");			} 			else // if it did not run ok			{ 			echo '<p>You could not be registered due to a system error. We apologise for any inconvenience.</p>'; 			}				}	} 	else // if one of the data tests failed	{ 	echo '<p>You have entered some data incorrectly. </p>';	}

Any help appreciated...Son

Link to comment
Share on other sites

Shouldn't this line:if (mysqli_num_rows($dbc, $emailResult) == 1) // email address is not availablebe:if (mysqli_num_rows($emailResult) == 1) // email address is not available?It's not pssible you've got more than one record with the same e-mail address due to testing before you added that check, is it?

Link to comment
Share on other sites

Shouldn't this line:if (mysqli_num_rows($dbc, $emailResult) == 1) // email address is not availablebe:if (mysqli_num_rows($emailResult) == 1) // email address is not available?It's not pssible you've got more than one record with the same e-mail address due to testing before you added that check, is it?
Realised that i made a mistake and corrected this to:if (mysqli_num_rows($emailResult) == 1) // email address is not availableStill same result (email address not twice in database, checked this directly via PhpMyAdmin). I just do not get it...Son
Link to comment
Share on other sites

I assume that when you check $first_name, $last_name and $email you have already pulled these values from the $_POST array and sanitized them etc.?

Link to comment
Share on other sites

Print the return value from mysqli_num_rows, see what it's returning.
When I insert:$ha = mysql_num_rows($emailResult); var_dump ($ha);it says that it is a false bool. My rountines to check for email is:
// check for an email	if (!isset($_POST['email']) OR empty($_POST['email'])) {		$email = FALSE;		$errors['email'] = '\'Email Address\' is a required field';		} else {			if (eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['email'])))) {				$email = escape_data($_POST['email']);				$email_echo = $email;				$emailCheck = $_POST['email'];			} else {			$email = FALSE;			$errors['email'] = '\'Email Address\' seems to be incorrect';			}		}

I tried to use $emailCheck with same result...Son

Link to comment
Share on other sites

Are you using the mysql or mysqli version? If it's returning false that means the query failed.
You got it straight to the point, I was using mysqli|-) I stared hours at the code and could not see this...Cheers, mate. Great help:-)Son
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...