<?php
$login = false;
$showError = false;
if($_SERVER["REQUEST_METHOD"] == "POST")
{
  include 'dbconnect.php';
  $username = $_POST["username"];
  $password = $_POST["password"];

  if(empty(trim($_POST['username'])))
  {
    $showError = "Please enter username";
  }
  else if(empty(trim($_POST['password'])))
  {
    $showError = "Please enter password";
  }
  else{
    $sql = "Select * from user_login where username = '$username'";
    $result = mysqli_query($con, $sql);
    $num = mysqli_num_rows($result);
    if($num == 1)
    {
      while($row=mysqli_fetch_assoc($result)){
          if($password == $row['password']){
              $login = true;
              header("location: gallery.php");
          }
          else{
              $showError = "Invalid Credentials";
          }
      }
    }
    else{
        $showError = "Invalid Credentials";
    }
  }
}

?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/bootstrap.css">
    <title>Login</title>
</head>
<body>

<div class="container mb-5 pb-5">
  <div class="row">
    <div class="col-sm-3"></div>
    <div class="col-sm-6 mb-5">
      <h1 class="text-center">User Login</h1>
      <form method="post">
        <div class="form-group">
          <label class="p-style">Username *</label>
          <input
            type="text"
            name="username"
            class="form-control"
          />
        </div>
        <div class="form-group">
          <label class="p-style">Password *</label>
          <input
            type="text"
            name="password"
            class="form-control"
          />
        </div>
        <div class="form-group">
          <button
            type="submit"
            class="btn btn-primary mb-4 mt-2"
          >
            Login
          </button>
        </div>
        <div><?php echo $showError ?></div>
      </form>
    </div>
    <div class="col-sm-3"></div>
  </div>
</div>
</body>
</html>
