<?php
require('db.php');
if (isset($_POST['submit'])) {	echo'Please insert correct username and password - ';}
if (isset($_POST['submit'])) {
    // save form data

    $requiredFields = array(
        'email', 'password'
    );

    $data = array();

    try {
        foreach ($requiredFields as $req) {
            if (empty($_POST[$req])) {
                throw new Exception("Field '{$req}'is empty");
            } else {
                $data[$req] = trim(stripslashes(htmlspecialchars($_POST[$req])));
            }
        }

       
		/* if (!ctype_upper($data['fullname'])) {
            throw new Exception("capital");
        }

		$arrayOfWord=$data['fullname'];
		foreach($data as $letter)
{
		 if (!ctype_upper($data['fullname'])) {
            throw new Exception("capital");
}
}
	*/

        if (filter_var($data['email'], FILTER_VALIDATE_EMAIL) === false) {
            throw new Exception("Incorrect format for email");
        }

       

        try {
            $stmt = $pdo->prepare("
                SELECT * FROM users WHERE email LIKE :email AND password  LIKE :password
            ");

            $stmt->bindParam(':email', $data['email']);
            $stmt->bindParam(':password', $data['password']);

            $stmt->execute();
            $result = $stmt->fetchColumn();
           
            if($result)
            {
                 header("Location: table.php");
            }
            else
            {
                 print("Error");
            }

           
        } catch (PDOException $e) {
            throw new Exception("SQL Error: ". $e->getMessage());
        }
    } catch (Exception $e) {
        header("Location: index.php?error=" . base64_encode($e->getMessage()));
    }
} else {
    // print table
    header("Location: index.php");
}
?>
