<?php
session_start();
if (isset($_SESSION['logged']) && $_SESSION['logged'] == "1" && $_SESSION['role'] == "admin") {

require_once('../../assets/constants/config.php');
require_once('../../assets/constants/uniques.php');


$fname = ucwords($_POST['fname']);
$lname = ucwords($_POST['lname']);
$gender = $_POST['gender'];
$email = $_POST['email'];
$dep = $_POST['department'];
$class = $_POST['class'];
$login_pass = password_hash($_POST['lpassword'], PASSWORD_DEFAULT);


try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8mb4;collation=utf8_general_ci;prefix=''", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

	
$stmt = $conn->prepare("SELECT email FROM tbl_admin WHERE email = :email UNION SELECT email FROM tbl_students WHERE email = :email");
$stmt->bindParam(':email', $email_address);
$stmt->execute();
$result = $stmt->fetchAll();
$rec = count($result);

if ($rec > 0) {

$_SESSION['reply'] = "012";
header("location:../students");

}else{

$stmt = $conn->prepare("INSERT INTO tbl_students (first_name, last_name, gender, department, class, email, login) 
	                    VALUES (:fname, :lname, :gender, :dep, :class, :email, :login)");
$stmt->bindParam(':fname', $fname);
$stmt->bindParam(':lname', $lname);
$stmt->bindParam(':gender', $gender);
$stmt->bindParam(':dep', $dep);
$stmt->bindParam(':class', $class);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':login', $login_pass);
$stmt->execute();

$_SESSION['reply'] = "013";
header("location:../students");

}

					  
}catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}


}else{

header("location:../");

}

?>
