<?php  //Start the Session

session_start();

 require('connectdb.php');
 require_once 'class.user.php';
 
 $reg_user = new USER();
 
 $username = "";

//3. If the form is submitted or not.
//3.1 If the form is submitted
if (isset($_POST['dat']) and isset($_POST['pw'])){
//3.1.1 Assigning posted values to variables.

$username = $_POST['dat'];
$upass = $_POST['pw'];
//3.1.2 Checking the values are existing in the database or not
$query = "SELECT * FROM members WHERE username = '$username' and password='$upass'";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
$count = mysqli_num_rows($result);


$stmt = $reg_user->runQuery("SELECT * FROM members WHERE username = '$username'");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);

$verified = 0;
$id = $row['id'];
//3.1.2 If the posted values are equal to the database values, then session will be created for the user.
if ($count == 0){
	$msg = "<div class='alert alert-danger'>
						<button class='close' data-dismiss='alert'>&times;</button>
						  Invalid Account Number or Password!
                   
			  </div>";
}

else{
//3.1.3 If the login credentials doesn't match, he will be shown with an error message.
$_SESSION['username'] = $username;
$_SESSION['id'] = $id;
$_SESSION['verified'] = $verified;
   // Redirect user to index.php
	    header("Location: ../../dashboard.php");
}
}
//3.1.4 if the user is logged in Greets the user with message

?>