· 8 years ago · May 20, 2018, 06:04 AM
1<?php include "database.php"; ?>
2<?php
3 function sanitise_input($data){
4 $data = trim($data);
5 $data = stripslashes($data);
6 $data = htmlspecialchars($data);
7 return $data;
8 }
9
10
11 if(isset($_POST['submit'])){
12 session_start();
13 // create array to hold errors
14
15 // If value increases we have a problem
16 $errors = 0;
17
18
19 // Check & Sanitize
20 $firstName = sanitise_input($_POST['name']);
21 $lastName = sanitise_input($_POST["lastname"]);
22 $studentID = sanitise_input($_POST["studentId"]);
23 $question1 = sanitise_input($_POST["json?"]);
24 $question2 = sanitise_input($_POST["question2"]);
25 $question3 = $_POST["OtherFormats"];
26 $question4 = $_POST["Specifications"];
27 $question5 = $_POST["data"];
28
29
30 //General Information StudentId, Names
31 if(empty($firstName)){
32 $_SESSION['name1'] = "Cannot be empty & Max of 25 Characters";
33 $errors = $errors + 1;
34 }
35 if(!preg_match("/^[- a-zA-Z]{1,25}+$/", $firstName)){
36 $_SESSION['name1'] = "Cannot be empty & Max of 25 Characters";
37 $errors = $errors + 1;
38 }
39
40 if(empty($lastName)){
41 $_SESSION['lastname'] = "Cannot be empty & Max of 25 Characters";
42 $errors = $errors + 1;
43 }
44 if(!preg_match("/^[- a-zA-Z]{1,25}+$/", $lastName)){
45 $_SESSION['lastname'] = "Cannot be empty & Max of 25 Characters";
46 $errors = $errors + 1;
47 }
48
49 if(empty($studentID)){
50 $_SESSION['studentId'] = "Cannot be empty & Max of 10 Characters";
51 $errors = $errors + 1;
52 }
53 if(!preg_match("/^\d{7,10}+$/", $studentID)) {
54 $_SESSION['studentId'] = "Cannot be empty & Max of 10 Characters";
55 $errors = $errors + 1;
56 }
57
58 // Questions 1 & 2
59 if(empty($_POST["json?"])) {
60 $_SESSION['json?'] = "Field Cannot be empty";
61 $errors = $errors + 1;
62 }
63 if(empty($_POST["question2"])) {
64 $_SESSION['question2'] = "Field Cannot be empty";
65 $errors = $errors + 1;
66 }
67
68 // Check Select, Radio, Checked
69 // Questions 3 & 4 & 5
70 if(!isset($_POST["OtherFormats"])) {
71 $_SESSION['OtherFormats'] = "select a field";
72 $errors = $errors + 1;
73 }
74 if(!isset($_POST["Specifications"])) {
75 $_SESSION['Question4a'] = "select a field";
76 $errors = $errors + 1;
77 }
78 // if(!isset($_POST["data"] )) {
79 // $_SESSION['Question5'] = "select a field";
80 // $errors++;
81 // }
82
83
84
85 // This line is where we will route user back, if validation fails.
86 if($errors != 0){
87 // We found errors send user back to quiz & allow them to re-enter values.
88 // Note - all form fields are empty for their own safety :D
89 $_SESSION['error'] = $errors;
90 header("location: quiz.php");
91 exit();
92 } else {
93 // We have found no errors
94 // This is where we Sanitize, Check marks, confirm attempts
95
96
97 // Check Attempts in database
98
99 // Get Attempts from database based off student ID
100 $query = "SELECT attempts FROM attempt WHERE studentNumber='$studentID'";
101 $result = $mysqli->query($query) or die($mysqli->error.__LINE__);
102 $row = $result->fetch_assoc();
103 $attempts = $row['attempts'];
104 if(!$row['attempts']){
105 $attempts = 1;
106 } else {
107 while($row = mysqli_fetch_assoc($result)){
108 $attempts = $row['attempts'];
109 }
110 }
111
112
113
114 // Check attempt value & increment
115 if($attempts == 3){
116 $_SESSION['attempts'] = "User has 3 attempts :D !! sorry...";
117 header("location: quiz.php");
118 exit();
119 }
120
121
122 // echo $question1."\n";
123 // echo $question2."\n";
124 // echo $question3."\n";
125 // echo $question4."\n";
126 // echo $question5;
127
128 // Calculate Score
129 $score = 0;
130 if(preg_match("/[Jj]ava[sS]cript\sobject\snotation/", $question1)){
131 $score = $score + 1;
132 }
133 if(preg_match("/(2|[Tt][wW][oO])/", $question2)){
134 $score = $score + 1;
135 }
136 if(preg_match("/[xX][mM][Ll]/", $question3)){
137 $score = $score + 1;
138 }
139 if(preg_match("/^[Dd]ouglas\s[Cc]rockford$/", $question4)){
140 $score = $score + 1;
141 }
142 if ($question5 == format) {
143 $score = $score + 1;
144 }
145
146 if($score == 0){
147 $_SESSION['attempts'] = "Dude you got zero!!!.";
148 header("location: quiz.php");
149 exit();
150 }
151
152 //Store data to table
153
154 // Check if student exists
155 $query = "SELECT studentNumber FROM attempt WHERE studentNumber='$studentID'";
156 $result = $mysqli->query($query) or die($mysqli->error.__LINE__);
157 $row = $result->fetch_assoc();
158 $student = $row['studentNumber'];
159
160
161 // $query = "SELECT score FROM attempt WHERE studentNumber='$studentID'";
162 // $res = $mysqli->query($query) or die($mysqli->error.__LINE__);
163 // $rows = $res->fetch_assoc();
164 // $currentScore = $rows['score'];
165 // $currentScore .= '|'.$score;
166
167
168
169 if(!$student){
170 $query = "INSERT INTO attempt (firstName, lastName, studentNumber, attempts, score) values ('$firstName', '$lastName', '$studentID', '$attempts', '$score')";
171 } else {
172 $attempts = $attempts + 1;
173 $query = "INSERT INTO attempt (firstName, lastName, studentNumber, attempts, score) values ('$firstName', '$lastName', '$studentID', '$attempts', '$score')";
174 }
175 $result = $mysqli->query($query) or die($mysqli->error.__LINE__);
176
177
178 if(!$result){
179 $_SESSION['error'] = "Query did not insert into table";
180 header("location: quiz.php");
181 exit();
182 }
183 }
184
185
186
187
188 }
189
190
191?>
192
193
194<!DOCTYPE html>
195<html lang="en">
196
197<head>
198 <meta charset="UTF-8" />
199 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
200 <meta http-equiv="X-UA-Compatible" content="ie=edge" />
201 <link rel="stylesheet" href="./styles/styles.css" />
202 <title>Results page</title>
203</head>
204
205<body id="resultspage">
206
207
208
209 <?php
210
211 $query = "SELECT * FROM attempt WHERE studentNumber='$studentID'";
212 $result = $mysqli->query($query) or die($mysqli->error.__LINE__);
213
214 echo "<table border=\"1\">\n";
215 echo "<tr>\n"
216 ."<th scope=\"col\">First Name</th>\n"
217 ."<th scope=\"col\">Second Name</th>\n"
218 ."<th scope=\"col\">Student ID</th>\n"
219 ."<th scope=\"col\">Attempts</th>\n"
220 ."<th scope=\"col\">Score</th>\n"
221 ."</tr>\n";
222 while ($row = mysqli_fetch_assoc($result)){
223 echo "<tr>\n";
224 echo "<td>",$row["firstName"],"</td>\n";
225 echo "<td>",$row["lastName"],"</td>\n";
226 echo "<td>",$row["studentNumber"],"</td>\n";
227 echo "<td>",$row["attempts"],"</td>\n";
228 echo "<td>",$row["score"],"</td>\n";
229 echo "</tr>\n";
230 }
231 echo "</table>\n";
232 echo "<a href='quiz.php' class='button'>Return To Quiz</a>";
233 ?>
234
235</body>
236
237
238</html>