title: I am trying to make a login page. However something keeps going wrong - and I don't know what?
body: <p>I'm new at PHP and I've been following a course on how to make a sign up/login page. I have successfully finished the signup page but the login page is giving me issues. Here is my code for my login.php:</p>
  <pre><code>&lt;?php
  include_once '../resources/session.php';
  include_once '../resources/database.php';
  include_once '../resources/utilities.php';
  
  if(isset($_POST['loginBtn'])){
  
      // array to hold errors
      $form_errors = array();
  
      // validate
      $required_fields = array('username ', 'password ');
  
      $form_errors = array_merge($form_errors, check_empty_fields($required_fields));
  
      if(empty($form_errors)){
  
          // collect form data
          $user = $_POST['username'];
          $password = $_POST['password'];
  
          //  check if user exists in the database
          $sqlQuery = &quot;SELECT * FROM users WHERE username = :username&quot;;
          $statement = $db-&gt;prepare($sqlQuery);
          $statement-&gt;execute(array(':username' =&gt; $user));
  
          while($row = $statement-&gt;fetch()){
              $id = $row['id'];
              $hashedpassword = $row['password'];
              $username = $row['username'];
  
              if(password_verify($password, $hashed_password)){
                  $_SESSION['id'] = $id;
                  $_SESSION['username'] = $username;
                  header(&quot;location: dashboard.php&quot;);
              }
              else{
                  $result =&quot;&lt;p style='padding: 20px; color: red; border: 1px solid gray;'&gt; Invalid username or password&lt;/p&gt;&quot;;
              }
          }
  
      }
      else{
          if(count($form_errors) == 1){
              $result = &quot;&lt;p style='color:red;'&gt;There was 1 error in the form&lt;/p&gt;&quot;;
          }
          else{
              $result = &quot;&lt;p style='color:red;'&gt; There were &quot; .count($form_errors). &quot; errors in the form &lt;/p&gt;&quot;;
          }
      }
  }
  
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;
      &lt;head&gt;
          &lt;meta charset=&quot;utf-8&quot;&gt;
          &lt;title&gt;Login&lt;/title&gt;
          &lt;link rel=&quot;stylesheet&quot; href=&quot;../css/indexstyles.css&quot;&gt;
      &lt;/head&gt;
      &lt;body&gt;
  
          &lt;h2&gt;Login Form&lt;/h2&gt;
          &lt;?php if(isset($result)) echo $result; ?&gt;
          &lt;?php if(!empty($form_errors)) echo show_errors($form_errors); ?&gt;
          &lt;form action=&quot;&quot; method=&quot;post&quot;&gt;
              &lt;table&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Username:&lt;/td&gt;
                      &lt;td&gt;&lt;input type=&quot;text&quot; value=&quot;&quot; name=&quot;username &quot;&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Password:&lt;/td&gt;
                      &lt;td&gt;&lt;input type=&quot;password&quot; value=&quot;&quot; name=&quot;password &quot;&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;input style=&quot;float: right;&quot; = type=&quot;submit&quot; name=&quot;loginBtn&quot; value=&quot;Login&quot;&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
          &lt;/form&gt;
  &lt;/html&gt;
  </code></pre>
  <p>My code for database.php is:</p>
  <pre><code>&lt;?php
  
  // intialize variables to hold connection parameters
  $username = 'root';
  $dsn = 'mysql:host=localhost; dbname=register';
  $password = 'xxxx';
  
  
  try{
      // create an instance of the PDO class with the required parameters
      $db = new PDO($dsn, $username, $password);
  
      // set PDO error mode to exception
      $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
      // display success message
      // echo &quot;Connected to the register database&quot;;
  
  }catch (PDOException $ex){
  
      // display error message
      echo &quot;Connection unsuccesful. ERROR: &quot;.$ex-&gt;getMessage();
  }
  
  </code></pre>
  <p>and my code for utilities.php is this:</p>
  <pre><code>&lt;?php
  
  /**
      * @param $required_fields_array, n array containing the list of all required fields
      * @return array, containing all errors
  */
  
  // start
  
   function check_empty_fields($required_fields_array){
          // initialize an array to store error messages
          $form_errors = array();
  
          // loop throgh the required fields array and popular the form error array
          foreach ($required_fields_array as $name_of_field){
              if(!isset($_POST[$name_of_field]) || $_POST[$name_of_field] == NULL){
                  $form_errors[] = $name_of_field . &quot;is a required field&quot;;
  
              }
  
          }
  
          return $form_errors;
  }
  
  /**
      * @param $fields_to_check_length, an array containing the name of fields
      * for which we wnt th check min required length e.g. array('username' =&gt; 4, 'email' =&gt; 12)
      * @return array, containing all errors
  */
  
  function check_min_length($fields_to_check_length){
      //initialize an array to store error messages
      $form_errors = array();
  
      foreach($fields_to_check_length as $name_of_field =&gt; $minimum_length_required){
          if(strlen(trim($_POST[$name_of_field])) &lt; $minimum_lenth_required){
              $form_errors[] = $name_of_field . &quot; is too short, must be {$minimum_length_required} characters long&quot;;
  
          }
      }
      return $form_errors;
  }
  
  
  /**
      * @param $data, store a key/value pair array where key is the name of the form control
      * in this case 'email' and value is the input entered by the user
      * @return array, containing email error
  */
  
  function check_email($data){
      //initialize an array to store error messages
      $form_errors + array();
      $key = 'email';
      // check if the key email exists in data array
      if(array_key_exists($key, $data)){
  
          // check if the email field has a value
          if($_POST[$key] != null){
  
              // remove all illegal characters from email
              $key = filter_var($key, FILTER_SANITIZE_EMAIL);
  
              // check if input is a valid email addresss
              if(filter_var($_POST[$key], FILTER_VALIDATE_EMAIL) === false){
                  $form_errors[] = $key . &quot; is not a valid email address&quot;;
              }
          }
      }
  
      return $form_errors;
  }
  
  /**
      * @param $form_errors_array, the array holding all
      * errors which we want to loop through
      * @return string, list containing all error messages
  */
  
  function show_errors($form_errors_array){
      $errors = &quot;&lt;p&gt;&lt;ul style ='color: red;'&gt;&quot;;
  
      // loop through error array and display all items in a list
      foreach($form_errors_array as $the_error){
          $errors .= &quot;&lt;li&gt; {$the_error} &lt;/li&gt;&quot;;
      }
      $errors .= &quot;&lt;/ul&gt;&lt;p&gt;&quot;;
      return $errors;
  }
  </code></pre>
  <p>Lastly, my sessions.php is:</p>
  <pre><code>&lt;?php
  session_start();
  </code></pre>
  <p>When I press the login button without entering anything, I get the expected error message. However, if I enter the demo account I made, I still get the same error message.</p>
  <p>Any help is greatly appreciated!</p>
  <ul>
  <li>Sianna</li>
  </ul>
  <p>A</p>