title: Having trouble creating a sign up/log in system with PDO statements in PHP
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>Here's the HTML part of the code:</p>
  
  <pre><code>&lt;form class="signUp" action="signup.inc.php" method="POST"&gt;
  &lt;input type="text" name="first" placeholder="First name" class="firstname" required&gt;
  &lt;input type="text" name="last" placeholder="Last name" class="lastname" required&gt;
  &lt;input type="text" name="email" placeholder="E-mail" class="email" required&gt;
  &lt;input type="text" name="username" placeholder="Username" class="username" required&gt;
  &lt;input type="password" name="password" placeholder="Password" class="password" required&gt;
  &lt;input type="submit" class="submit" name="submit" value="Sign up"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And here's the poor attempt of PHP validation using PDO statements (obviously it isn't working):</p>
  
  <pre><code>&lt;?php
  
  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");
      exit();
  } else{
      if (!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)){
          header("Location: signup.php?signup=invalid");
          exit();
      } else{
          if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
              header("Location: signup.php?signup=invalidEmail");
              exit();
          } else{
              $query = $db-&gt;prepare("SELECT * FROM users WHERE username = :username");
              $query-&gt;execute(['username' =&gt; $username]);
              $results = $query-&gt;fetch();
  
              if ($query-&gt;rowCount()&gt;0){
                  header("Location: signup.php?signup=usernameTaken");
                  exit();
              } else{
                  $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
                  $query = $db-&gt;prepare("INSERT INTO users (firstname, lastname, email, username, password) VALUES ('$first', '$last', '$email', '$username', '$hashedPassword'))";
                  $query-&gt;execute();
                  header("Location: signup.php?signup=success");
                  exit();
              }
          }
      }
  }
  
  } else{
  header("Location: signup.php");
  exit();
  }
  </code></pre>
  
  <p>Thank you!</p>