diff --git a/answer_script.php b/answer_script.php index cfa8360..7f70bb1 100644 --- a/answer_script.php +++ b/answer_script.php @@ -3,120 +3,117 @@ require_once "assets/connect/pdo.php"; if (!isset($_SESSION['Student_ID']) && !isset($_SESSION['Batch']) && !isset($_SESSION['Section'])) { - header("Location: student_login.php"); - return; + header("Location: student_login.php"); + return; } -$question_id = $_GET['id']; -$title = $_GET['title']; -$course_title = $_GET['ct']; -$course_code = $_GET['cc']; -$batch = $_GET['batch']; -$section = $_GET['sec']; - // Variables declared as empty for persisting data on the form -$name = $student_id = $batch = $section = $success = $failed = $reg_done = ''; +$name = $student_id = $batch = $section = $success = $failed = $answer = ''; // errors array to put all the error message in the array -$errors = array('name' => '', 'student_id' => '', 'batch' => '', 'section' => ''); - -// Validating and setting data in variables from first form - -if (isset($_POST["submit"])) { - - //check name - if (empty($_POST['name'])) { - $errors['name'] = 'A name is required'; - } else { - $name = $_POST['name']; - if (!preg_match('/^[a-zA-Z\s]+$/', $name)) { - $errors['name'] = 'Name must be letters and spaces only'; - } - } - - //student id check - if (empty($_POST['student_id'])) { - $errors['student_id'] = 'Student ID is required.'; - } else { - $student_id = $_POST['student_id']; - if (!preg_match('/^[0-9]*$/', $student_id)) { - $errors['student_id'] = 'ID must be numbers only.'; - } else if ($_POST['student_id'] != $_SESSION['Student_ID']) { - $errors['student_id'] = 'You cannot put others student ID.'; - } - } +$errors = array('name' => '', 'student_id' => '', 'batch' => '', 'section' => '', 'answer' => ''); - //batch check - if (empty($_POST['batch'])) { - $errors['batch'] = 'Batch is required.'; - } else { - $batch = $_POST['batch']; - if (!preg_match('/^[0-9]*$/', $batch)) { - $errors['batch'] = 'Batch must be numbers only.'; - } else if ($_POST['batch'] != $_SESSION['Batch']) { - $errors['batch'] = 'You should put your batch only'; - } - } - //section check - if (empty($_POST['section'])) { - $errors['section'] = 'Section is required.'; - } else { - $section = $_POST['section']; - if (!preg_match('/^[a-zA-Z\s]+$/', $section)) { - $errors['section'] = 'Section must be a character.'; - } else if ($_POST['section'] != $_SESSION['Section']) { - $errors['section'] = 'You should put your section only (case-sensitive)'; - } - } - if (array_filter($errors)) { - //echo 'errors in form'; - } else { - //setting info in variables here - $name = $_POST['name']; - $student_id = $_POST['student_id']; - $batch = $_POST['batch']; - $section = $_POST['section']; - - $reg_done = ""; - } -} //fetching questions and other data if (isset($_GET['id'])) { - $question_id = $_GET['id']; - require_once "assets/connect/pdo.php"; - $stmt = $pdo->query("SELECT * FROM question_description WHERE Question_Description_ID = $question_id"); + $question_id = $_GET['id']; + require_once "assets/connect/pdo.php"; + $stmt = $pdo->query("SELECT * FROM question_description WHERE Question_Description_ID = $question_id"); - $infos = $stmt->fetchAll(PDO::FETCH_ASSOC); + $infos = $stmt->fetchAll(PDO::FETCH_ASSOC); } + + + //inserting answer in database if (isset($_POST["ansSubmit"])) { - $ansName = $_POST['ansName']; - $ansId = $_POST['ansId']; - $ansBatch = $_POST['ansBatch']; - $ansSec = $_POST['ansSec']; - $ansQuestion_ID = $_POST['ansQuestion_ID']; + $name = $_POST['name']; + $student_id = $_POST['student_id']; + $batch = $_POST['batch']; + $section = $_POST['section']; + $answer = $_POST['answer']; + $question_id = $_POST['question_id']; + + + + //name check + if (empty($_POST['name'])) { + $errors['name'] = 'A name is required'; + } else { + $name = $_POST['name']; + if (!preg_match('/^[a-zA-Z\s]+$/', $name)) { + $errors['name'] = 'Name must be letters and spaces only'; + } + } + + //student id check + if (empty($_POST['student_id'])) { + $errors['student_id'] = 'Student ID is required.'; + } else { + $student_id = $_POST['student_id']; + if (!preg_match('/^[0-9]*$/', $student_id)) { + $errors['student_id'] = 'ID must be numbers only.'; + } else if ($_POST['student_id'] != $_SESSION['Student_ID']) { + $errors['student_id'] = 'You should put your student ID only.'; + } + } + + //batch check + if (empty($_POST['batch'])) { + $errors['batch'] = 'Batch is required.'; + } else { + $batch = $_POST['batch']; + if (!preg_match('/^[0-9]*$/', $batch)) { + $errors['batch'] = 'Batch must be numbers only.'; + } else if ($_POST['batch'] != $_SESSION['Batch']) { + $errors['batch'] = 'You should put your batch only'; + } + } + + //section check + if (empty($_POST['section'])) { + $errors['section'] = 'Section is required.'; + } else { + $section = $_POST['section']; + if (!preg_match('/^[a-zA-Z\s]+$/', $section)) { + $errors['section'] = 'Section must be a character.'; + } else if ($_POST['section'] != $_SESSION['Section']) { + $errors['section'] = 'You should put your section only (case-sensitive)'; + } + } + + //answer field check + if (empty($_POST['answer'])) { + $errors['answer'] = 'Answer field cannot be empty.'; + } else { $answer = $_POST['answer']; + } - try { - require_once "assets/connect/pdo.php"; + if (array_filter($errors)) { + //echo 'errors in form'; + } else { - $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $sql = "INSERT INTO student_answer (Full_Name, Student_ID, Batch, Section, Question_Description_ID, Answer) VALUES('$ansName', '$ansId', '$ansBatch', '$ansSec', '$ansQuestion_ID', '$answer')"; - // use exec() because no results are returned - $pdo->exec($sql); - $success = ""; + $name = $_POST['name']; + $student_id = $_POST['student_id']; + $batch = $_POST['batch']; + $section = $_POST['section']; + $answer = $_POST['answer']; + $question_id = $_POST['question_id']; + + + try { + $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $sql = "INSERT INTO student_answer (Full_Name, Student_ID, Batch, Section, Question_Description_ID, Answer) VALUES('$name', '$student_id', '$batch', '$section', '$question_id', '$answer')"; + // use exec() because no results are returned + $pdo->exec($sql); + $success = ""; } catch (PDOException $e) { - $err = $e->getMessage(); - $failed = ""; + $err = $e->getMessage(); + $failed = ""; } $ownBatch = $_SESSION['Batch']; @@ -124,6 +121,11 @@ $_SESSION['ExamDone'] = "Thank you for attending the Exam. Your answer script has been recieved."; sleep(2); header("Location: student_dashboard.php?batch=$ownBatch&sec=$ownSection"); + } + // echo '
'; + // var_dump($infos); + // echo ''; + } ?> @@ -133,16 +135,16 @@ + require_once 'assets/connect/head.php'; + require_once 'assets/summer_Note/summer_Note.php'; + ?>
Don't forget to attach your details first before handing over your answer script.
- +Fill in the form first before handing over to the answer script.
- -