<?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');

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 * FROM tbl_school_info");
    $stmt->execute();
    $result = $stmt->fetchAll();
    
    foreach($result as $row) {

        $currency = $row[6];
        date_default_timezone_set($row[5]);

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

$name = ucwords($_POST['name']);
$dep = $_POST['department'];
$class = $_POST['class'];
$reg_date = date('Y-m-d G:i:s');

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 * FROM tbl_subjects WHERE name = :name AND department = :dep AND class = :class");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':dep', $dep);
$stmt->bindParam(':class', $class);
$stmt->execute();
$result = $stmt->fetchAll();
$rec = count($result);

if ($rec > 0) {

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

}else{

$stmt = $conn->prepare("INSERT INTO tbl_subjects (name, department, class, reg_date) VALUES (:name, :dep, :class, :reg_date)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':dep', $dep);
$stmt->bindParam(':class', $class);
$stmt->bindParam(':reg_date', $reg_date);
$stmt->execute();

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

}

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

}else{

header("location:../");

}

?>
