-2

In creating a registration page for a project I'm working on. It's very basic, just requires the user to enter a username and password but I want the user to confirm their password. I'm using:

$pattern = '/^[0-9]{6,}$/';

to ensure that only digits can be entered as a password and must be at least 6 digits long. Everything is working fine except for confirming password. I have attached my script below. Can anyone see what I'm doing wrong? When I enter two different values in the password boxes on my register.html page, it seems to just accept and enters the values to the database regardless.

    $username = $_POST['username']; //username
    $password = $_POST['password']; //password
    $confirmPass = $_POST['password2'];
    $error = array();
    $pattern = '/^[0-9]{6,}$/'; //pattern for password

if($_POST['register']){ //if register is clicked
    if(!preg_match($pattern, $password)){ //is password doesnt match...
        echo "Password must be at least 6 characters long and numerical characters only."; //error message
    }

$query = "SELECT * FROM users WHERE username = '$username'"; //calls exising profiles
$result = mysqli_query($conn, $query);
    if(mysqli_num_rows($result)>0){
        echo "You already have an account!"; //if username existsm error message displayed
    } else {

    }

if($password === $confirmPass){ //if two passwords do not match
    echo 'Passwords do not match!';
} else {
       if(preg_match($pattern, $password)){ //if passwords match
    $password = md5($confirmPass); //md5 encryption
    $query = "INSERT INTO users (username, password) VALUES ('$username', '$password')"; //inserts into the database
        echo'You are now a registered user!';
    if($conn->query($query) === TRUE){
    } else {
        echo"Error: " . $query / "<br>" . $conn->error;
    }
} 
}

}

Aaron Martin
  • 128
  • 1
  • 13

1 Answers1

0
if($password === $confirmPass){ //if two passwords do not match
    echo 'Passwords do not match!';
} else {

This is incorrect. You're actually checking if they do match.

if($password != $confirmPass){ //if two passwords do not match
    echo 'Passwords do not match!';
} else {

This should fix the problem.

rickdenhaan
  • 10,857
  • 28
  • 37
  • Probably worth mentioned the difference between `!==` and `!=` and `===` and `==`. – Script47 Apr 28 '18 at 23:58
  • @Script47 True, but instead of adding it to my answer I'll just [refer to this excellent explanation](https://stackoverflow.com/a/80649/1941241). – rickdenhaan Apr 29 '18 at 00:01