0

I am trying to make a registration and login form for my college assignment but the registration form doesn't seem detect a password in the password field, it always throws the 'A password is needed' error, which is what it should do if nothing is entered into the field.

Here's the code: server.php:

<?php
    $username = '';
    $email = '';
    $errors = array();


    //connect to the database
    $db = mysql_connect('localhost', 'root', '', 'registration');

    //if the register button is clicked
    if (isset($_POST['register'])) {
        $username = mysql_real_escape_string($_POST['username']);
        $email = mysql_real_escape_string($_POST['email']);
        $password_1 = mysql_real_escape_string($_POST['password_1']);
        $password_2 = mysql_real_escape_string($_POST['password_2']);


        //ensure that form fields are filled properly
        if (empty($username)) {
            array_push($errors, 'Username is required');
        }

        if (empty($email)) {
            array_push($errors, 'An Email is required');
         }

         if (empty($password_1)) {
             array_push($errors, 'A password is required');
         }

         if ($password_1 != $password_2) {
             array_push($errors, 'The two passwords do not match');
         }

         if (count($errors) == 0) {
             $password = md5($password_1); 
             $sql = "INSERT INTO users (username, email, password) 
                       VALUES ('$username', '$email', '$password')";

             mysql_query($db, $sql);
          }
    }
?>

register.php:

<?php include('server.php'); ?>
<!DOCTYPE html>
<html>
<head>
    <title>User Registration using PHP and MySQL</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div class="header">
        <h2>Register</h2>
    </div>

    <form method="post" action="register.php">
    <?php include('errors.php'); ?>
        <div class="input-group">
            <label>Username</label>
            <input type="text" name="username" value="<?php echo "$username";?>">
        </div>
        <div class="input-group">
            <label>Email</label>
            <input type="text" name="email" value="<?php echo "$email";?>">
        </div>
        <div class="input-group">
            <label>Password</label>
            <input type="password" name="password">
        </div>
        <div class="input-group">
            <label>Confirm password</label>
            <input type="password" name="password">
        </div>
        <div class="input-group">
            <button type="submit" name="register" class="btn">Register</button>
        </div>
        <p>
            Already have an account? <a href="login.php">Sign in</a>
        </p>
    </form>

</body>
</html>
Flame Foxx
  • 61
  • 1
  • 4

2 Answers2

0

You have to match your input tags on the form with your POST tags on the PHP side. If you call your input tag 'password' then you have to reference it as such on your PHP side as $_POST['password'] but you are calling them as $_POST['password_1']

jmedrano
  • 1
  • 3
0

In the form you have fields named "password" in the code you are looking for password_2 and password_1. You need to change the HTML form to use password_2 and password_1. So:

    <div class="input-group">
        <label>Password</label>
        <input type="password" name="password_1">
    </div>
    <div class="input-group">
        <label>Confirm password</label>
        <input type="password" name="password_2">
    </div>
GrabzIt
  • 41
  • 4