<?php
	
	session_start();

	include "API.php";

    $isU = 0;
    
	$sql = "SELECT id, lastname, firstname, password FROM teachers WHERE id = '".$_POST['form_id']."'";
	$recordSet =& $dbconn->Execute($sql);

	if (!$recordSet)
	{
		echo $dbconn->ErrorMsg();
	}
	else
	{
		list($id, $lastname, $firstname, $password) = $recordSet->fields;
	}
	
	$num_of_records = $recordSet->RecordCount();
	
    // ΑΝ ΤΟ $num_of_records ΕΙΝΑΙ 0, ΤΟΤΕ ΔΕΝ ΥΠΑΡΧΕΙ ΑΥΤΟΣ Ο ΧΡΗΣΤΗΣ
	if ($num_of_records == 0) 
	{
		$error = 1;
	}
	else
	{
		// ΑΝ ΤΟ password ΠΟΥ ΕΔΩΣΕ Ο ΧΡΗΣΤΗΣ ΕΙΝΑΙ ΣΩΣΤΟ....
		if ($password == $_POST['form_pass']) 
		{
			$isU = 1;	
		}
		else
		{
			$error = 2;
		}
	}

	
	//Finally if login successfull, redirect to main page, else login page again
	if ($isU) 
	{
		$_SESSION['teacher_id'] = $id;
        $_SESSION['isUser'] = $isU;
        $_SESSION['name'] = $firstname.' '.$lastname;

        
		Header("Location:main.php");
	}
	else
	{
		Header("Location:index.php?error=$error");
	}




?>
