Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi all,

 

I am going through Chapter 8 of "php6 and mysql5" now.

 

I am wondering people can get a custom error message when they register inputting the email address that is already existing in database? I added UNIQUE index on email in mysql so that I can prevent it from having duplicate email addresses.

 

They would get a default error message. However, I want to customize it so that end users only can see the message. Such as "The email address you input is already existing. Please try a different email."

 

I tried to manipulate the codes but kept getting error messages and not working properly.

 

What I did is below(script 8.5 - register.php);

 

-------------------------------------------------------------------------------------------------------------------

.

.

.

 

$sem = "SELECT email FROM users";

$rc = @mysqli_query($dbc, $sem);

 

//check for an email address;

if(!empty($_POST['email'])) {

 

if($_POST['email'] = $rc){

echo"<p>The email address you input is already existing. Please try a different email</p>\n";

}else {

$e=mysqli_real_escape_string($dbc, trim($_POST['email']));

}

}else{

$errors[]='You forgot to enter your email address';

}

 

.

.

.

.

 

------------------------------------------------------------------------------------------------------------------

 

Thank you very much for your help in advance and look forward to hearing from you soon.

Link to comment
Share on other sites

Hi Ryan R,

 

What you could do here is query the database with the email specified.

 

Then:

if (mysqli_num_rows($r) != 0) {
    echo 'This email is already in the databse';
} else {
// proceed to insert email into databse
}

 

Hope that helps ;)

 

Let me know

  • Upvote 1
Link to comment
Share on other sites

Hi Jonathon,

 

I thought your suggestion would definitely work.. Maybe I am putting it to a wrong spot. Can you help me with this again? I will attach the whole codes that I made here.

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------

 

<?php #script 8.5 - register.php #2

 

$page_title='register';

include('includes/header.html');

 

//check if the form has been submitted;

if(isset($_POST['submitted'])) {

 

require_once('../../mysqli_connect.php');//connect to the db

 

$errors=array(); // Initialize an error array;

 

//check for a first name;

if(empty($_POST['first_name'])) {

$errors[]='You forgot to enter your first name.';

} else {

$fn=mysqli_real_escape_string($dbc, trim($_POST['first_name']));

}

 

//check for a last name;

if(empty($_POST['last_name'])) {

$errors[]='You forgot to enter your last name';

}else{

$ln=mysqli_real_escape_string($dbc, trim($_POST['last_name']));

}

 

//check for an email address;

 

if(empty($_POST['email'])) {

$errors[]='You forgot to enter your email address';

}else {

$e=mysqli_real_escape_string($dbc, trim($_POST['email']));

}

 

//check for a password and match against the confirmed password;

if(!empty($_POST['pass1'])){

if($_POST['pass1'] != $_POST['pass2']) {

$errors[] = 'Your password did not match the confirmed password';

}else {

$p=mysqli_real_escape_string($dbc, trim($_POST['pass1']));

}

}else {

$errors[]='You forgot to enter your password';

}

 

if(empty($errors)) { // If everything is OK.

//register the user in the database..

 

//make the query

$q="INSERT INTO users (first_name,last_name,email,pass,registration_date) VALUES('$fn','$ln','$e',SHA1('$p'),NOW())";

$r=@mysqli_query($dbc, $q);//run the query;

 

if($r) {//if it ran OK

//print the message.

echo'<h1>Thank you!</h1>

<p>You are now registered. In Chapter 11 you will actually be able to log in!</p><p></br></p>';

 

}else{//if it didn not run OK.

 

if(mysqli_num_rows($r) != 0) {

echo'<p>Your email is already registered in the database. Please try a different one.</p>';

} else {

echo'<h1>System Error</h1>

<p class="error">You could not be registered due to a system error. We appologize for any inconvenience.</p>';

//debugging message;

echo'<p>'.mysqli_error($dbc).'</p>';

}

 

} // end of if($r);

 

 

mysqli_close($dbc); //close the database connection.

//include the footer and quit the script;

include('includes/footer.html');

exit();

} else { // report the errors;

echo'<h1>Error!</h1>

<p class="error">The following error(s) occured:<br>';

foreach($errors as $msg) {//print the each error.

echo "- $msg<br>\n";

}

echo '</p><p>Please try again</p><p><br></p>';

}//end of if(empty($errors))IF.

 

mysqli_close($dbc); // close the database connection

 

}//End of the main submit conditional.

 

 

?>

 

<h1>Register</h1>

<form action="register.php" method="post">

<p>First name: <input type="text" name="first_name" size="15" maxlength="20" value="<?php if(isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>

<p>Last name: <input type="text" name="last_name" size="15" maxlength="20" value="<?php if(isset($_POST['last_name'])) echo $_post['last_name']; ?>" /></p>

<p>Email Address: <input type="text" name="email" size="20" maxlength="80" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" /></p>

<p>Password: <input type="password" name="pass1" size="10" maxlength="20" /></p>

<p>Confirm Password: <input type="password" name="pass2" size="10" maxlength="20" /></p>

<P><input type="submit" name="submit" value="Register" /></P>

<p><input type="hidden" name="submitted" value="TRUE" /></p>

 

</form>

 

<?php

include('includes/footer.html');

?>

 

---------------------------------------------------------------------------------------------------------------------------------------------------------------

 

I put the codes that you advised me. You can look at the codes in bold.

 

Thanks again for your kind cooperation and look forward to hearing from you soon.

Link to comment
Share on other sites

Hi Ryan,

 

Yep I can try help you figure it out. What I would say is wrong here is that you are running the mysqli_num_rows query in the wrong place.

 

You need to take the input from the form, in particulary the email and query the database to see if it returned any rows.

 

This is because if it doesnt return 0 rows then the email alaready exists in the database so you don't need to bother processing the form.

 

If it does return 0 then it's ok to try and add the record as you know there is nobody else in the system registered with that email.

 

So in short process all the inputs such as:

if(empty($_POST['email'])) { 
$errors[]='You forgot to enter your email address';
}else { 
$e=mysqli_real_escape_string($dbc, trim($_POST['email']));
}

 

Then if there ok

 

Query the database to see if the email supplied exists.

 

Then once you've done this query run the

if (mysqli_num_rows($r) !=0)) {

 

Then if the query does return 0 then use the big query you used to submit the data to the databse as you have validated all the inputs and checked that the email doesn't exist either.

 

Let me know how you get on too. ;)

  • Upvote 1
Link to comment
Share on other sites

//run the query;

$r=@mysqli_query($dbc, $q);

 

//if it ran OK

if($r) {

//print the message.

echo'<h1>Thank you!</h1>

<p>You are now registered. In Chapter 11 you will actually be able to log in!</p><p></br></p>';

 

} else { // if it didn not run OK.

 

if(mysqli_num_rows($r) != 0) {

echo'<p>Your email is already registered in the database. Please try a different one.</p>';

} else {

echo'<h1>System Error</h1>

<p class="error">You could not be registered due to a system error. We appologize for any inconvenience.</p>';

//debugging message;

echo'<p>'.mysqli_error($dbc).'</p>';

}

}

 

The problem is the testing of the variable $r

 

if ($r) {
  // returns TRUE on queries POSSIBLE to run.
} else {
  // returns FALSE on query FAILURE (Mysql error) 
}

 

As your query (hopefully) is written correctly, the TRUE clause will always apply.

 

mysql_num_rows() is a function that checks how many rows was AFFECTED by your MySQL query.

 

- Lets say you add one user to the table users successfully by $r=mysqli_query($dbc, $q);

- The number of rows inserted to the table is now 1 (one)

- This means, mysql_num_rows($r) will be equal to 1 (one)

 

What's the point of mysql_num_rows?:

While the code if ($r) { } will only check if the query is true... (It gets TRUE/FALSE from mysqli_query($dbc, $q);

The code if (mysql_num_rows($r = 1)) { } will test for interacting with the table data

 

Hope this clears something up. :)

  • Upvote 1
Link to comment
Share on other sites

  • 2 weeks later...
 Share

×
×
  • Create New...