Jump to content
Larry Ullman's Book Forums

Recommended Posts

Could anyone help. I keep getting the message "You forgot to enter your email address" when I have. Also the forms not sticky.

<?php # script 8.3 = register.php
$page_title = 'Register';
include ('includes/header.html');
// check if the form has been submitted:
if (isset($_POST['submitted'])) {

$errors = array(); // initianalise an error array:

// Check for first name:
if (empty($_POST['first_name'])) {
 $errors[] = 'You forgot to enter your First Name.';
} else {
 $fn = trim($_POST['first_name']);
}

// Check for last name:
if (empty($_POST['last_name'])) {
 $errors[] = 'You forgot to enter your Last Name.';
} else {
 $ln = trim($_POST['last_name']);
}

// Check for an email address:
if (empty($_POST['email'])) {
 $errors[] = 'You forgot to enter your Email Address.';
} else {
 $e = trim($_POST['email']);
}

// Check for password and match against the confirmed password:
if (empty($_POST['pass1'])) {
 if ($_POST['pass1'] != $_POST['pass2']) {
  $errors[] = 'Your password did not match your confirmed password.';
 } else {
  $p = trim($_POST['pass1']);
 }
} else {
 $errors[] = 'You forgot to enter your password.';
}

if (empty($errors)) { // if everythings OK

// Register the user in the database

require_once ('mysqli_connect.php'); // Connect to the database.

// make the query:
$q = "INSERT INTO l30trsb_jobsite (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 for registering!</h1>
<p>You are now registered. In chapter 11 you will actually be able to login!</p><p><br /></p>';

} else { // If it did not run OK:

// Public message:
echo '<h1>System Error</h1>
<p class="error">You could not be registered. Please try again in a short while.</p>';

// Debugging message:
echo '<p>' .mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';

}// End of if ($r) IF.

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 each error.
echo " . $msg<br />\n";
}
echo '</p><p>Please try again.</p><p><br /></p>';

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

} // End of main submit conditional
?>
 <h1>Register</h1>
 <form action="register.php" method="post">
 <p>First Name: <input type="text" name="first_name" size="15" maxlengh="20" value="<?php if (isset($POST['first_name'])) echo $_POST['first_name']; ?>" /> </p>
 <p>Last Name: <input type="text" name="last_name" size="15" maxlengh="40" value="<?php if (isset($POST['last_name'])) echo $_POST['last_name']; ?>" /> </p>
 <p>Email Address: <input type="text" name="email" size="20" maxlengh="80" value="<?php if (isset($POST['email'])) echo $_POST['email']; ?>" /> </p>
 <p>Password: <input type="password" name="pass1" size="10" maxlengh="20" /></p>
 <p>Confirm Password: <input type="password" name="pass2" size="10" maxlengh="20" /></p>
 <p><input type="submit" name="submit" value="Register" /><p/>
 <input type="hidden" name="submitted" value="TRUE" />
 </form>
 <?php
 include ('includes/footer.html');
 ?>

Link to comment
Share on other sites

Your insert query will not work for precisely the reason that benjamin.morgan indicated, though that would not explain why you are getting the email error message. However, I'm surprised you're not getting an error regarding the password.

// Check for password and match against the confirmed password:
if (empty($_POST['pass1'])) {
if ($_POST['pass1'] != $_POST['pass2']) {
$errors[] = 'Your password did not match your confirmed password.';
} else {
$p = trim($_POST['pass1']);
}
} else {
$errors[] = 'You forgot to enter your password.';
}

The logic here is not correct, you want to check initially for a NOT empty pass1 e.g.

// Check for password and match against the confirmed password:
if (!empty($_POST['pass1'])) {
if ($_POST['pass1'] != $_POST['pass2']) {
$errors[] = 'Your password did not match your confirmed password.';
} else {
$p = trim($_POST['pass1']);
}
} else {
$errors[] = 'You forgot to enter your password.';
}

  • Upvote 1
Link to comment
Share on other sites

Thanks it's now fixed.

 

Edward you were right POST_ was missing.

 

Benjamin yes the $'in' was wrong it should be '$in'

 

Margaux the problem was indeed this line. if (!empty($_POST['pass1'])) {

 

As soon as I corrected those it all worked fine. I do think that if you do get a problem with the code you either spot it straight away or you spend that much time looking at it you completely look straight through the error.

 

When you get to the stage where you can’t see what’s wrong I think it’s best another pair of eyes look through it.

 

Sorry for the confusion with the original post it was the "Password" that threw up the error not the "email" as I said at first.

 

Thanks again

 

Paul

  • Upvote 1
Link to comment
Share on other sites

It does help for another set of eyes to look at it, but it also helps to read over it a few times, then take a break, and a couple of days later come back and study it, if you still can't find anything, dissect it and take it one line at a time! Read the logic out loud to make sure that is what you wanted. It has really helped me find errors in the code this way. :)

Link to comment
Share on other sites

 Share

×
×
  • Create New...