· 8 years ago · May 26, 2018, 07:24 PM
1<!DOCTYPE html>
2<html>
3<head>
4 <title>MarkQuiz</title>
5
6
7<link rel="stylesheet" type="text/css" href="styles/style.css">
8</head>
9<body>
10<?php
11 require_once ("settings.php"); // Connection info
12
13 $conn = @mysqli_connect($host,
14 $user,
15 $pwd,
16 $sql_db
17 );
18
19////////////////////////////////////////////////////////////
20
21 function sanitise_input($data) {
22 $data = trim($data);
23 $data = stripslashes($data);
24 $data = htmlspecialchars($data);
25 return $data;
26 }
27
28 $errMsg = "";
29
30
31////////////////////////////////////////////////////////////
32
33 if(isset ($_POST["firstname"])) {
34 $firstname = $_POST["firstname"];
35 $firstname = sanitise_input($firstname);
36
37 if($firstname = "") {
38 $errMsg .= "<p>you must enter your first name.</p>";
39 }
40 elseif (!preg_match("/^[a-zA-Z]*$/", $firstname)) {
41 $errMsg .= "<p>Only alpha characters allowed</p>";
42 }
43 }
44
45
46 if(isset ($_POST["lastname"])) {
47 $lastname = $_POST["lastname"];
48 $lastname = sanitise_input($lastname);
49
50 if($lastname = "") {
51 $errMsg .= "<p>you must enter your last name</p>";
52 }
53 elseif (!preg_match("/^[a-zA-Z]*$/", $lastname)) {
54 $errMsg .= "<p>Only alpha characters allowed</p>";
55 }
56 }
57
58
59 if(isset ($_POST["studentnumber"])) {
60 $studentid = $_POST["studentnumber"];
61 $studentid = sanitise_input($studentid);
62
63 if($studentid = "") {
64 $errMsg .= "<p>you must enter your last name</p>";
65 }
66 elseif (!preg_match("/^[0-9]{7}$|^[0-9]{10}$/", $studentid)) {
67 $errMsg .= "<p>Your Student ID needs to be between 7-10</p>";
68 }
69 }
70
71 if ($errMsg != ""){
72 echo "<p>$errMsg</p>";
73}
74
75///////////////////////////////////////////////////////////
76///Create table with no attempt counter
77
78$query = "CREATE TABLE IF NOT EXISTS attempts (
79 attempt_id INT AUTO_INCREMENT PRIMARY KEY,
80 studentid INT,
81 firstname VARCHAR(30) NOT NULL,
82 lastname VARCHAR(30) NOT NULL ,
83 score VARCHAR(5) NOT NULL
84);";
85
86$result = mysqli_query($conn, $query);
87
88////////////////////////////////////////////////////////////
89//Check if attempts is < 3
90$attemptcount = 0;
91
92 if (isset($_POST["studentnumber"])) {
93 $studentid = $_POST["studentnumber"];
94 }
95
96 $query = "SELECT COUNT(attempt_id) AS attemptcounter FROM $sql_table WHERE studentid = '$studentid';";
97
98 if ($result = mysqli_query($conn, $query)) {
99 $attemptRow = mysqli_fetch_accoc($result)
100 $attemptCount = $attempt['attemptcounter']
101 }
102
103mysqli_close($conn);
104
105///////////////////////////////////////////////////////////
106
107$score = 0;
108
109if (isset ($_POST["question1"])) {
110 $question1 = $_POST["question1"];
111 $question1 = sanitise_input($question1);
112 }
113
114if (isset ($_POST["question2"])) {
115 $question2 = $_POST["question2"];
116 }
117
118if (isset ($_POST["question3"])) {
119 $question3 = $_POST["question3"];
120 }
121
122
123if (isset ($_POST["question4"])) {
124 $question4 = $_POST["question4"];
125 }
126
127if (isset ($_POST["question5"])) {
128 $question5 = $_POST["question5"];
129 }
130
131if ($question1 == "facebook") {
132 $score = $score + 20;
133}
134
135if ($question2 == "python") {
136 $score = $score + 20;
137}
138
139if ($question3 == "singlepage"){
140 $score = $score + 20;
141
142}
143
144if ($question4 == "search") {
145 $score = $score + 20;
146}
147
148if ($question5 == "one") {
149 $score = $score + 20;
150
151}
152
153///////////////////////////////////////////////////////////
154
155 // Checks if connection is succesful
156 if (!$conn) {
157 // Displays an error message
158 echo "<p>Database connection failure</p>"; //not in production script
159 } else {
160 //upon succesful connection
161 $sql_table="attempts";
162 $studentid = trim($_POST["studentnumber"]);
163 $firstname = trim($_POST["firstname"]);
164 $lastname = trim($_POST["lastname"]);
165
166
167 $query = "insert into $sql_table (studentid, firstname, lastname, score) values ('$studentid', '$firstname', '$lastname', $score)";
168
169 $result = mysqli_query($conn, $query);
170
171 echo "<p>result :", $result, "</p>";
172
173 if(!$result) {
174 echo "<p class=\"wrong\">something is wrong with ", $query, "</p>";
175 } else {
176 echo "<p class\"ok\">Succesfully added new record</p>";
177 }
178
179 mysqli_close($conn);
180}
181///////////////////////////////////////////////////////////
182
183
184require_once ("settings.php"); // Connection info
185
186 $conn = @mysqli_connect($host,
187 $user,
188 $pwd,
189 $sql_db
190 );
191 // Checks if connection is succesful
192 if (!$conn) {
193 // Displays an error message
194 echo "<p>Database connection failure</p>"; //not in production script
195 } else {
196 //upon succesful connection
197 $sql_table="attempts";
198
199 //set up the SQL command to query or add data into the table
200 $query = "SELECT attempt_id, studentid, firstname, lastname, score, Count(attempt_id) AS attemptcount FROM attempts";
201
202 //execute the query and store result into the result pointer
203 $result = mysqli_query($conn, $query);
204
205 //checks if the execution was succesful
206 if(!$result) {
207 echo "<p> something is wrong with", $query, "</p>";
208 } else {
209 //display the retrieved records
210 echo "<table border=\"1\">\n";
211 echo "<tr>\n "
212 ."<th scope=\"col\">attempt_id</th>\n "
213 ."<th scope=\"col\">studentid</th>\n "
214 ."<th scope=\"col\">firstname</th>\n "
215 ."<th scope=\"col\">lastname</th>\n "
216 ."<th scope=\"col\">score</th>\n "
217 ."<th scope=\"col\">attemptcount</th>\n "
218 ."<tr>\n ";
219 // retrieve current record pointed by the result pointer
220 while ($row = mysqli_fetch_assoc($result)){
221 echo "<tr>\n";
222 echo "<td>", $row["attempt_id"], "</td>\n";
223 echo "<td>", $row["studentid"], "</td>\n";
224 echo "<td>", $row["firstname"], "</td>\n";
225 echo "<td>", $row["lastname"], "</td>\n";
226 echo "<td>", $row["score"], "</td>\n";
227 echo "<td>", $row["attemptcount"], "</td>\n";
228 echo "</tr>\n ";
229 }
230 echo "</table>\n "<!DOCTYPE html>