简体   繁体   中英

Register page not registering user

I have been through this code again and again manually and using code checkers but cannot see my error. I am building a register page which should give errors if any of the fields are left blank or passwords do not match. The form looks just fine, but when I try to register I get the error message "The following errors occurred - enter your password. Please try again." This happens regardless of what I enter as the password.

Also, the form should remember the data that has been entered when the error message is given, but for some reason it will fill in last name, and e-mail fields, but not first name. Again, when looking at the script I cannot see why that is.

I am learning from a basic book - PHP & MySQL in easy steps, which might not be the best out there, but I want to finish it before moving onto something else.

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Providing a register page</title>
</head>
<body>
<?php
$page_title = 'Register' ;
include ( 'includes/header.html' );

if ( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' ) {
    require ( '../connect_db.php' );
    $errors = array() ;

    if ( empty( $_POST[ 'first_name' ] ) ) {
        $errors[] = 'Enter your first name.';
    } else {
        $fn = mysqli_real_escape_string($dbc, trim( $_POST[ 'first_name' ] ) );
    }

    if ( empty( $_POST[ 'last_name' ] ) ) {
        $errors[] = 'Enter your last name.';
    } else {
        $ln = mysqli_real_escape_string($dbc, trim( $_POST[ 'last_name' ] ) );
    }

    if ( empty( $_POST[ 'email' ] ) ) {
        $errors[] = 'Enter your email address.';
    } else {
        $e = mysqli_real_escape_string($dbc, trim( $_POST[ 'email' ] ) );
    }

    if ( !empty($_POST[ 'pass1' ] ) ) {
        if ( $_POST[ 'pass1' ] != $_POST[ 'pass2' ] ) {
            $errors[] = 'Passwords do not match.';
        } else {
            $p = mysqli_real_escape_string( $dbc, trim( $_POST[ 'pass1' ] ) );
        }
    } else {
        $errors[] = 'Enter your password.';
    }
    if ( empty( $errors ) ) {
        $q = "SELECT user_id FROM users WHERE email='$e'";
        $r = mysqli_query ( $dbc , $q );
            if ( mysqli_num_rows( $r ) != 0 ) {
                $errors[] = 'Email address already registered. <a href="login.php">Login</a>';
            }
    }

    if ( empty( $errors ) ) {
        $q = "INSERT INTO users (first_name, last_name, email, pass, reg_date) VALUES ('$fn', '$ln', '$e', SHA1('$p'), NOW()";
        $r = mysqli_query ( $dbc , $q );

        if ( $r ) {
            echo '<h1>Registered!</h1>'
                .'<p>You are now registered.</p>'
                .'<p><a href="login.php">Login</a></p>';
        }
    mysqli_close( $dbc ) ;
    include ( 'includes/footer.html' );
    exit();
} else {
    echo '<h1>Error!</h1>'
        .'<p id="err_msg">The following error(s) occurred:<br>';
    foreach ( $errors as $msg ) {
        echo " - $msg<br>";
    }
    echo 'Please try again.</p>';
    mysqli_close( $dbc );
}
?>

<h1>Register</h1>
<form action="register.php" method="POST">
    <p>
    First Name: <input type="text" name="first_name"
        value="<?php if ( isset( $_POST[ ' first_name' ] )) echo $_POST[ 'first_name' ] ; ?>">
    Last Name: <input type="text" name="last_name"
        value="<?php if ( isset( $_POST[ 'last_name' ] )) echo $_POST[ 'last_name' ] ; ?>">
    </p><p>
    Email address: <input type="text" name="email"
        value="<?php if ( isset( $_POST[ 'email' ] )) echo $_POST[ 'email' ] ; ?>">
    </p><p>
    Password: <input type="password name="pass1"
        value="<?php if ( isset( $_POST[ 'pass1' ] )) echo $_POST[ 'pass1' ] ; ?>"> 
    Password: <input type="password name="pass2"
        value="<?php if ( isset( $_POST[ 'pass2' ] )) echo $_POST[ 'pass2' ] ; ?>"> 
    </p><p>
    <input type="submit" value="Register"> </p>
</form>
<?php include ( 'includes/footer.html' ) ; ?>
</body>
</html>

You forget a Parenthesis " ) "

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM