Having trouble creating a sign up/log in system with PDO statements in PHP

4355 views php

I tried creating a sign up/log in system using PHP and MySQL but stumbled on a problem with PDO statements. Can't get the "fetch" part of the code to work since I'm not familiar with PDO statements so I would appreciate some help. Also, I would greatly appreciate an explanation of how "placeholder" variables work in PDO statements and how do we check if there is already a username inside a database with the "fetch" method in PDO statements.

MySQL database is fairly simple. It includes an auto-incrementing "id" (primary key) field, a "firstname" field, a "lastname" field, an "email" field, a "username" field and "password" field, respectively.

Here's the HTML part of the code:

<form class="signUp" action="signup.inc.php" method="POST">
<input type="text" name="first" placeholder="First name" class="firstname" required>
<input type="text" name="last" placeholder="Last name" class="lastname" required>
<input type="text" name="email" placeholder="E-mail" class="email" required>
<input type="text" name="username" placeholder="Username" class="username" required>
<input type="password" name="password" placeholder="Password" class="password" required>
<input type="submit" class="submit" name="submit" value="Sign up">

And here's the poor attempt of PHP validation using PDO statements (obviously it isn't working):


if (isset($_POST['submit'])){
include_once 'init.php';

$first = $_POST['first'];
$last = $_POST['last']
$email = $_POST['email']
$username = $_POST['username']
$password = $_POST['password']

if (empty($first) || empty($last) || empty($email) || empty($username) || empty($password)){
    header("Location: signup.php?signup=empty");
} else{
    if (!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)){
        header("Location: signup.php?signup=invalid");
    } else{
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
            header("Location: signup.php?signup=invalidEmail");
        } else{
            $query = $db->prepare("SELECT * FROM users WHERE username = :username");
            $query->execute(['username' => $username]);
            $results = $query->fetch();

            if ($query->rowCount()>0){
                header("Location: signup.php?signup=usernameTaken");
            } else{
                $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
                $query = $db->prepare("INSERT INTO users (firstname, lastname, email, username, password) VALUES ('$first', '$last', '$email', '$username', '$hashedPassword'))";
                header("Location: signup.php?signup=success");

} else{
header("Location: signup.php");

Thank you!

answered question

1 Answer


You are not binding parameters. This is where placeholder enter the conversation. A placeholder ? is used for binding parameters. You cannot prepare a statement with php variables, but only with placeholders. Have a look at the rewrite of the corresponding piece of your code:

// note that the 
$query = $db->prepare("INSERT INTO users (firstname, lastname, email, username, password) VALUES (?, ?, ?, ?, ?)");
$query->bindParam(':firstname', $first);
$query->bindParam(':lastname', $last);
$query->bindParam(':email', $email);
$query->bindParam(':username', $username);
$query->bindParam(':password', $hashedPassword);

Unless you want to learn the steps in validation by your self because you are eager to lern yourself, you might want to think about looking at a library like GUMP for validation.

posted this

Have an answer?


Please login first before posting an answer.