Jump to content
Larry Ullman's Book Forums

Recommended Posts

I am having trouble getting the errors to populate on my register script. I copied the code from chapter 9 explicitly and I am continuing to overlook what is causing the problem. The problem is that the correct error message does not populate, such as when there is no last name or email address, instead of expressing that, it just says that I have an error and does not say what the error is for; such as an empty field. mysql error messages work just fine.

 

 

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        
        $errors = array(); //will handle/store every error msg, one for each form input improperly filled out
        
        if (empty($_POST['first_name'])) {
            $errors[] = 'you forgot to enter your first name.';
        } else {
            $fn = trim($_POST['first_name']);
        }
        
        if (empty($_POST['last_name'])) {
            $errors[] = 'you forgot to enter your last name.';
        } else {
            $ln = trim($_POST['last_name']);
        }
        
        if (empty($_POST['email'])) {            
            $errors[] = 'you forgot to enter a email.';
        } else {
            $e = trim($_POST['email']);
        }
        
        if (!empty($_POST['pass1'])) {
            if ($_POST['pass1'] != $_POST['pass2']) {
            $errors[] = 'your passwords do not match.';
            } else {
            $p = trim($_POST['pass1']);
            }
        } else {            
            $errors[] = 'you forgot to enter your password.';
        }
        
        
    if (empty($errors)) { // if there are no errors
    
        // register the user in the database
                
            require ('mysqli_connect.php'); // connect to database
            
             //make the query
        $q = "INSERT INTO users_info (first_name, last_name, email, pass1, time)
        VALUES ('$fn', '$ln', '$e', SHA1('$p'), NOW() )";
        $r = @mysqli_query ($dbc, $q); // run the query
        if ($r) { // if everything is ok
        
            //print message:
            echo '<h3>Thank You!</h3>
            <p>you are now registered.</p>
            <p><br /></p>';
        
        } else {
            //public msg
            echo '<h2>system error</h2>
            <p class="error"> you could not register due to system error. sorry for the inconvenience.</p>';
        
            //debugging msg
            echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';
        }   //end of $r IF
        
        //close db connection
        mysqli_close($dbc);
        exit();
        
        } else { //return error
            
            echo '<h1>Error!</h1>
            <p class="error">The following error(s) occurred:<br />';
            foreach ($error as $msg) { // print each error.
                echo " - $msg<br />\n";
            }
            echo '</p><p>Please try again.</p><p><br /><p>';
        
        } // end of if (empty($error)) IF.    
        
    }

Link to comment
Share on other sites

You have a typo where you foreach your errors. Instead of foreaching $errors, you foreach $error in your code. The loop will thus not run, and no errors will be displayed.

 

Thanx, I looked at this script numerous of times with no avail, appreciate that much.

Link to comment
Share on other sites

 Share

×
×
  • Create New...