<?php
////////////////////// POST ACTION TO CHECK STUDENTS RESULT //////////////////////
if (isset($_POST['check_result_btn'])) {
    $result_regno = inject_checker($connection, $_POST['result_regno']);
    $result_class = inject_checker($connection, $_POST['result_class']);
    $result_term = inject_checker($connection, $_POST['result_term']);
    $result_session = inject_checker($connection, $_POST['result_session']);

    // Error Checking for Empty Fields
    if (empty($result_regno)) {
        $_SESSION['msg'] = "Error! Registration Number is Required";
        $_SESSION['type'] = "error";
    } elseif ($result_class == $select) {
        $_SESSION['msg'] = "Error! Please Select Class to check Result";
        $_SESSION['type'] = "error";
    } elseif ($result_term == $select) {
        $_SESSION['msg'] = "Error! Please Select the term for the Result";
        $_SESSION['type'] = "error";
    } else {
        $query = "SELECT * FROM `results1` WHERE `reg_number` = '{$result_regno}' AND `class` = '{$result_class}' AND `term` = '{$result_term}' AND `session` = '{$result_session}'";
        $run_query = mysqli_query($connection, $query);

        if ($run_query) {
            if (mysqli_num_rows($run_query) > 0) {
                $result = mysqli_fetch_assoc($run_query);
                $_SESSION['result'] = $result['id'];

                // Redirect to result page
                echo "<script type='text/javascript'>
                        window.location.href = 'admin_result_page.php?print=true';
                      </script>";
                exit;
            } else {
                $_SESSION['msg'] = "No Result Records Found!";
                $_SESSION['type'] = "error";
            }
        } else {
            $_SESSION['msg'] = "Result Checking Failed!";
            $_SESSION['type'] = "error";
        }
    }
     header("Location: admin_check_result");
            exit();
}
?>

