<?php
session_start();
include('config.php') ;
echo "<br>";

$username=$_POST['username'];
$password=$_POST['password'];


$sql="select * from users where username='$username'";
$result=mysqli_query($mysql_connection, $sql);

if(mysqli_num_rows($result)>0){

    while($row=mysqli_fetch_assoc($result)){
        /*
        Δοκιμές για να δω αν έρχονται

        echo "ID: " . $row["userID"]. ", ";
        echo "Όνομα: " . $row["name"]. ", ";
        echo "email: " . $row["email"]. ", ";
        echo "Ρόλος: " . $row["role"]. ", ";
        echo "<br>";
        */

        $userID=$row["userID"];
        $username=$row["username"];
        $psswrd=$row["password"];
        $name=$row["name"];
        $email=$row["email"];
        $role=$row["role"];
       
        /*
        header("Location: index.php");
        */

    }if ($password != $psswrd){


        $message = "Ο κωδικός είναι λάθος!";
        echo "<script type='text/javascript'>alert('$message');</script>";
        echo "<script type='text/javascript'>setTimeout(function() { window.location.href = 'index.php'; }, 10);</script>";


    }else{
        $_SESSION['userID']=$userID;
        $_SESSION['username']=$username;
        $_SESSION['password']=$psswrd;
        $_SESSION['name']=$name;
        $_SESSION['email']=$email;
        $_SESSION['role']=$role;

        header("Location: index.php");
        
    }


}else{



$message = "Δεν υπάρχει εγγεγραμένος με αυτό το όνομα χρήστη!";
echo "<script type='text/javascript'>alert('$message');</script>";
echo "<script type='text/javascript'>setTimeout(function() { window.location.href = 'index.php'; }, 10);</script>";

}

?>




<script>
function redirectToIndex() {
    window.location.href = 'index.php';
}
</script>

<!--
<script>redirectToIndex()</script>

<script>
function redirectToIndex() {
    window.location.href = 'index.php';
}
</script>
-->
