<?php include"inc/conn.php";
session_start();
// initializing variables
$username = "";
$password   = "";
$errors = array(); 

//userlogin
  if (isset($_POST['login_user'])) {
  $username  = mysqli_real_escape_string($db, $_POST['username']);
  $password = mysqli_real_escape_string($db, $_POST['password']);

  if  (empty($username )) {
    array_push($errors, "username  is required");
  }
  if (empty($password)) {
    array_push($errors, "Password is required");
  }
     
   else if (count($errors) == 0) {
    $password = md5($password);
    $query = "SELECT * FROM users WHERE Username ='$username' AND Password='$password'";
    $results = mysqli_query($db, $query);
    
    if (mysqli_num_rows($results) > 0) {
      $_SESSION['username'] = $username;
     
    $query2 = "SELECT Role FROM users WHERE Username='$username'";
    $results2 = mysqli_query($db, $query2);
    $row = mysqli_fetch_object($results2);
   if($row->Role=='Admin'){
      header('location: index.php'); 
    }
     else{ header('location:staffs');
   }
     
    }else {
      array_push($errors, "invalid username  or password !!!");
    }
  }
}
?>