<?php

    include($_SERVER['DOCUMENT_ROOT'] ."/_ps2020/config/connect.php");

    $username = $_POST["email"];
    $password = $_POST["password"];
    //To check if username is valid or not

    $show_error = true;

    $result = $conn->query("SELECT * FROM user_account");

    //First we check if that actually exists or not
    if($result){

        while($row = $result->fetch_assoc()){

            //echo to check if username is valid or invalid
            if($row["email"] == $username){
                //So we have found a matching username

                //Now we check if the respective password is correct or not

                if($row["password"] == $password){
                    //That means we have found the match
                    //Now we need to create a session and direct the user to the page

                    session_start();
                    $_SESSION["email"] = $username;
                    $_SESSION["username"] = $row["username"];
                    //This link need to be changed
                    header('location: '.dir_root.'account/dashboard.php');

                }
            }
            
            if($row["username"] == $username){
                
                //So now we have found the matching username
                //Now we search for the password

                if($row["password"] == $password){
                    //That means all the user input are correct!!

                    session_start();
                    $_SESSION["username"] = $username;
                    $_SESSION["email"] = $row["email"];
                    //This link needs to be changed
                    header('location: '.dir_root.'account/dashboard.php');

                }
            }

        }
    }

    echo "No user found with those credentials!!";

    $conn->close();

?>