<?php
session_start();
error_reporting(0);
if (isset($_POST['username']) && isset($_POST['password'])) {
  $username = $_POST['username'];
  $password = trim($_POST['password']);


  if ($password != '' && $username != '') {

      require 'connect.php'; 
      //================== GET DATA VERIFIED ==================================
      $data = mysqli_query($connect, "SELECT * FROM customers WHERE Username = '$username'");
      $numRows = mysqli_num_rows($data);

      if ($numRows > 0) {
        $fetchData = mysqli_fetch_assoc($data);
        $DBpassword = $fetchData['Password'];
        if ($DBpassword === $password) {
           $updateLastLogin = mysqli_query($connect, "UPDATE customers SET LastLogin = NOW() WHERE ID = ".$fetchData['ID']."");
           if ($updateLastLogin) {
            $_SESSION['UserID'] = $fetchData['ID'];
            echo "success";
           }else {
              echo '<p class="error">Error: Could not log you in at this moment</p>';
           }
                     
        }else {
          echo '<p class="error"> Error: Invalid credentials </p>';
        }

      }else {
        echo '<p class="error"> Error: Invalid credentials </p>';
      }
  }else {
    echo '<p class="error"> Both fields must not be empty.. </p>';
  }






}else {
  header("Location: /personal/");
  die();
}