title: login returns no user but I have users in database
body: <p>I am trying to create a user login system for a website and users can register but when I try to login it shows an error message that there is no user by that email. Not sure what the problem is . Here is the code for login. Help please.
  this part is part if index.php</p>
  
  <pre><code>&lt;?php 
       if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      if (isset($_POST['login'])) { //user logging in
          require 'login.php';        
      }   
      elseif (isset($_POST['register'])) { //user registering        
          require 'register.php';        
      }}
     ?&gt;
  
      &lt;div id="login" class="col-md-12"&gt;
      Login via
          &lt;div class="social-buttons"&gt;
              &lt;a href="#" class="btn btn-fb"&gt;&lt;i class="fa fa-facebook"&gt;&lt;/i&gt;&lt;/a&gt;
              &lt;a href="#" class="btn btn-tw"&gt;&lt;i class="fa fa-google-plus"&gt;&lt;/i&gt; &lt;/a&gt;
          &lt;/div&gt;
          or
       &lt;form  method="post" action="index.php" accept-charset="UTF-8" autocomplete="off"&gt;
              &lt;div class="form-group"&gt;
                   &lt;label class="sr-only"  for="loginInputEmail"&gt;Email address&lt;/label&gt;
                   &lt;input type="email" required class="form-control"  id="loginInputEmail" placeholder="Email address" &gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                   &lt;label class="sr-only" for="loginInputPassword"&gt;Password&lt;/label&gt;
                   &lt;input type="password" required  class="form-control" id="loginInputPassword2" placeholder="Password" &gt;
                   &lt;div class="help-block text-right"&gt;&lt;a href="includes/forgot.php"&gt;Forgot your password?&lt;/a&gt;&lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
  
                   &lt;button class="button button-block" name='login' /&gt;Log In&lt;/button&gt;
              &lt;/div&gt;
              &lt;div class="checkbox"&gt;
                   &lt;label&gt;
                   &lt;input type="checkbox"&gt; keep me logged-in
                   &lt;/label&gt;
              &lt;/div&gt;
       &lt;/form&gt;
       New here? Join Us
  
      &lt;/div&gt;
      &lt;div id="signup" style="display:none;" class="col-md-12"&gt;
      &lt;form method="post" action="index.php" accept-charset="UTF-8" autocomplete="off" &gt;
          &lt;div class="form-group"&gt;
               &lt;label class="sr-only" for="regInputFName"&gt;First Name&lt;/label&gt;
               &lt;input type="text" required class="form-control" name ='firstname' id="regInputFName" placeholder="First Name" &gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
               &lt;label class="sr-only" for="lastName"&gt;Last Name&lt;/label&gt;
               &lt;input type="lastName" required class="form-control" name='lastname' id="lastName" placeholder="Last Name" &gt;
  
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
               &lt;label class="sr-only" for="loginInputEmail"&gt;Email address&lt;/label&gt;
               &lt;input type="email" required class="form-control" name='email' id="loginInputEmail" placeholder="Email address" &gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
               &lt;label class="sr-only" for="loginInputPassword"&gt;Password&lt;/label&gt;
               &lt;input type="password" required class="form-control" name='password' id="loginInputPassword2" placeholder="Password" &gt;
               &lt;div class="help-block text-right"&gt;&lt;a href="includes/forgot.php"&gt;Forgot your password?&lt;/a&gt;&lt;/div&gt;
          &lt;/div&gt;
          --&lt;div class="form-group"&gt;
  
              &lt;button type="submit" class="button button-block" name="register" /&gt;Register&lt;/button&gt; 
          &lt;/div&gt;
          Member? Sign in!                
       &lt;/form&gt;                                                                        
      &lt;/div&gt;              
      &lt;/div&gt;
  </code></pre>
  
  <p>and the following code is the login.php</p>
  
  <pre><code>&lt;?php
      $email = $mysqli-&gt;escape_string($_POST['email']);
  $result = $mysqli-&gt;query("SELECT * FROM users WHERE email='$email'");
  
  if ( $result-&gt;num_rows == 0 ){ 
      $_SESSION['message'] = "User with that email doesn't exist!";
      header("location: error.php");
  }
  else { 
      $user = $result-&gt;fetch_assoc();
      if ( password_verify($_POST['password'], $user['password']) ) {        
          $_SESSION['email'] = $user['email'];
          $_SESSION['first_name'] = $user['first_name'];
          $_SESSION['last_name'] = $user['last_name'];
          $_SESSION['active'] = $user['active'];        
          $_SESSION['logged_in'] = true;
          header("location: profile.php");
      }
      else {
          $_SESSION['message'] = "You have entered wrong password, try again!";
          header("location: error.php");
      }
  }
  </code></pre>
  
  <p>And finally the following is the register.php code that that works fine and creates user info in the database in phpmyadmin users database.</p>
  
  <pre><code>&lt;?php
  $_SESSION['email'] = $_POST['email'];
  $_SESSION['first_name'] = $_POST['firstname'];
  $_SESSION['last_name'] = $_POST['lastname'];
  
  $first_name = $mysqli-&gt;escape_string($_POST['firstname']);
  $last_name = $mysqli-&gt;escape_string($_POST['lastname']);
  $email = $mysqli-&gt;escape_string($_POST['email']);
  $password = $mysqli-&gt;escape_string(password_hash($_POST['password'], PASSWORD_BCRYPT));
  $hash = $mysqli-&gt;escape_string( md5( rand(0,1000) ) );
  
  $result = $mysqli-&gt;query("SELECT * FROM users WHERE email='$email'") or die($mysqli-&gt;error());
  
  if ( $result-&gt;num_rows &gt; 0 ) {    
      $_SESSION['message'] = 'User with this email already exists!';
      header("location: error.php");
  
  }
  else { 
      $sql = "INSERT INTO users (first_name, last_name, email, password, hash) " 
              . "VALUES ('$first_name','$last_name','$email','$password', '$hash')";
  
      if ( $mysqli-&gt;query($sql) ){
          $_SESSION['active'] = 0; //0 until user activates their account with verify.php
          $_SESSION['logged_in'] = true; // So we know the user has logged in
          $_SESSION['message'] =                
                   "Confirmation link has been sent to $email, please verify
                   your account by clicking on the link in the message!";
  
          $to      = $email;
          $subject = 'Account Verification ';
          $message_body = '
          Hello '.$first_name.',
          Thank you for signing up!
          Please click this link to activate your account:
          http://localhost/login-system/verify.php?email='.$email.'&amp;hash='.$hash;  
          mail( $to, $subject, $message_body );
          header("location: profile.php"); 
      }
  
      else {
          $_SESSION['message'] = 'Registration failed!';
          header("location: error.php");
      }
  }
  </code></pre>
  
  <p>I appreciate any help to fix the problem with loging in and the data is there in myphpmydmin.</p>