· 6 years ago · Jul 05, 2019, 05:50 AM
1$score = $_session['score']
2
3<?php include "database.php"; ?>
4
5<?php
6 //Get the total questions
7 $query="select * from questions";
8 //Get Results
9 $results = $mysqli->query($query) or die ($mysqli->error.__LINE__);
10 $total = $results->num_rows;
11
12 ?>
13<!DOCTYPE html>
14<html>
15 <head>
16 <meta charset="utf-8" />
17 <?php include "nav-bar.php" ?>
18 <title>Quizzer!</title>
19 <link rel="stylesheet" href="style.css" type="text/css" />
20
21 </head>
22 <body>
23 <div id="container">
24 <header>
25 <div class="container">
26 <br>
27 <br>
28 <br>
29 <h1>LaBoa weekly quiz</h1>
30 </div>
31 </header>
32
33
34 <main>
35 <div class="container">
36 <br>
37 <h2>Test your knowlege</h2>
38 <br>
39 <p>This is a multiple choice quize to test your knowledge about something</p>
40 <ul>
41 <li><strong>Number of Questions: </strong><?php echo $total; ?></ul>
42 <li><strong>Type: </strong>Multiple Choice</ul>
43 <li><strong>Estimated Time: </strong><?php echo $total*0.5; ?> minutes</ul>
44 </ul>
45 <a href="question.php?n=1" class="start">Start Quiz</a>
46 </div>
47 </div>
48 </main>
49
50
51 <footer>
52 <div class="container">
53 Copyright © 2019, LaBoa
54 </div>
55 </footer>
56 </body>
57</html>
58
59<?php include 'database.php'; ?>
60<?php session_start(); ?>
61<?php
62
63 //Check to see if score is set_error_handler
64 if (!isset($_SESSION['score'])){
65 $_SESSION['score'] = 0;
66 }
67
68//Check if form was submitted
69if($_POST){
70 $number = $_POST['number'];
71 $selected_choice = $_POST['choice'];
72 $next=$number+1;
73 $total=4;
74
75 //Get total number of questions
76 $query="SELECT * FROM `questions`";
77 $results = $mysqli->query($query) or die($mysqli->error.__LINE__);
78 $total=$results->num_rows;
79
80 //Get correct choice
81 $q = "select * from `choices` where question_number = $number and is_correct=1";
82 $result = $mysqli->query($q) or die($mysqli->error.__LINE__);
83 $row = $result->fetch_assoc();
84 $correct_choice=$row['id'];
85
86
87
88 //compare answer with result
89 if($correct_choice == $selected_choice){
90 $_SESSION['score']++;
91 }
92
93 if($number == $total){
94 $score = $_POST['score'];
95 header("Location: final.php");
96 exit();
97 } else {
98 header("Location: question.php?n=".$next."&score=".$_SESSION['score']);
99 }
100}
101?>
102
103<?php include "database.php"; ?>
104<?php session_start(); ?>
105<?php
106 //Create Select Query
107 ?>
108<!DOCTYPE html>
109<html>
110 <head>
111 <meta charset="utf-8" />
112 <?php include "nav-bar.php" ?>
113 <title>
114 Quizzer!</title>
115 <link rel="stylesheet" href="style.css" type="text/css" />
116 </head>
117 <body>
118 <div id="container">
119 <header>
120 <div class="container">
121 <br>
122 <br>
123 <br>
124 <h1>LaBoa Weekly Quiz</h1>
125 </div>
126
127 </header>
128
129
130 <main>
131 <div class="container">
132 <h2>You are Done!</h2>
133 <p>Congrats! You have completed the test</p>
134 <p>Final socre: <?php echo $_SESSION['score']; ?></p>
135 <a href="question.php?n=1" class="start">Take Test Again</a>
136 <?php
137 $query="INSERT into results (account_id, score, score_date)
138 values('name', 'score', date)";
139 ?>
140 <?php session_destroy(); ?>
141 </div>
142 </main>
143
144
145 <footer>
146 <div class="container">
147 Copyright © 2019, LaBoa
148 </div>
149 </footer>
150 </body>
151</html>
152
153-- Table structure for table `questions`
154--
155
156CREATE TABLE `questions` (
157 `question_number` int(11) NOT NULL,
158 `question` text COLLATE utf16_bin NOT NULL
159) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_bin;
160
161--
162
163-- Table structure for table `results`
164--
165
166CREATE TABLE `results` (
167 `result_id` int(11) NOT NULL,
168 `account_id` int(11) NOT NULL,
169 `score` int(11) NOT NULL,
170 `score_date` date NOT NULL
171) ENGINE=InnoDB DEFAULT CHARSET=utf8;
172
173CREATE TABLE `users` (
174 `id` int(11) NOT NULL,
175 `firstname` varchar(50) NOT NULL,
176 `lastname` varchar(50) NOT NULL,
177 `username` varchar(50) NOT NULL,
178 `password` varchar(255) NOT NULL,
179 `email` varchar(100) NOT NULL,
180 `img` text NOT NULL
181) ENGINE=InnoDB DEFAULT CHARSET=utf8;
182
183<?php
184// Change this to your connection info.
185$DB_HOST = 'localhost';
186$DB_USER = 'root';
187$DB_PASS = '';
188$DB_NAME = 'phplogin';
189// Try and connect using the info above.
190$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
191if ($mysqli->connect_errno) {
192 // If there is an error with the connection, stop the script and display the error.
193 die ('Failed to connect to MySQL: ' . $mysqli->connect_errno);
194}
195// Now we check if the data was submitted, isset will check if the data exists.
196if (!isset($_POST['firstname'], $_POST['lastname'], $_POST['username'], $_POST['password'], $_POST['email'])) {
197 // Could not get the data that should have been sent.
198 die ('Please complete the registration form!<br><a href="register.html">Back</a>');
199}
200// Make sure the submitted registration values are not empty.
201if (empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['username']) || empty($_POST['password']) || empty($_POST['email'])) {
202 // One or more values are empty...
203 die ('Please complete the registration form!<br><a href="register.html">Back</a>');
204}
205// We need to check if the st_account with that username exists
206if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
207 die ('Email is not valid!<br><a href="register.html">Back</a>');
208 if (preg_match('/[A-Za-z0-9]+/', $_POST['username']) == 0) {
209 die ('Username is not valid!<br><a href="register.html">Back</a>');
210}
211if (strlen($_POST['password']) > 20 || strlen($_POST['password']) < 5) {
212 die ('Password must be between 5 and 20 characters long.<br><a href="register.html">Back</a>');
213}
214}
215if ($stmt = $mysqli->prepare('SELECT id, password FROM users WHERE username = ?')) {
216 // Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
217 $stmt->bind_param('s', $_POST['username']);
218 $stmt->execute();
219 $stmt->store_result();
220 // Store the result so we can check if the st_account exists in the database.
221 if ($stmt->num_rows > 0) {
222 // Username already exists
223 echo 'Username exists, please choose another!<br><a href="register.html">Back</a>';
224 } else {
225 // Username doesnt exists, insert new st_account
226 if ($stmt = $mysqli->prepare('INSERT INTO users (firstname, lastname, username, password, email) VALUES (?, ?, ?, ?, ?)')) {
227 // We do not want to expose passwords in our database, so hash the password and use password_verify when a user logs in.
228 $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
229 $stmt->bind_param('sssss', $_POST['firstname'], $_POST['lastname'], $_POST['username'], $password, $_POST['email']);
230 $stmt->execute();
231 echo 'You have successfully registered, you can now login!<br><a href="index.html">Login</a>';
232 } else {
233 echo 'Could not prepare statement!';
234 }
235 }
236 $stmt->close();
237} else {
238 echo 'Could not prepare statement!';
239}
240$mysqli->close();
241?>
242
243<?php
244session_start();
245// Change this to your connection info.
246$DB_HOST = 'localhost';
247$DB_USER = 'root';
248$DB_PASS = '';
249$DB_NAME = 'phplogin';
250// Try and connect using the info above.
251$con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
252if ( mysqli_connect_errno() ) {
253 // If there is an error with the connection, stop the script and display the error.
254 die ('Failed to connect to MySQL: ' . mysqli_connect_error());
255}
256// Now we check if the data was submitted, isset will check if the data exists.
257if ( !isset($_POST['username'], $_POST['password']) ) {
258 // Could not get the data that should have been sent.
259 die ('Username and/or password does not exist!');
260}
261// Prepare our SQL
262if ($stmt = $con->prepare('SELECT id, password FROM users WHERE username = ?')) {
263 // Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
264 $stmt->bind_param('s', $_POST['username']);
265 $stmt->execute();
266 $stmt->store_result();
267 // Store the result so we can check if the st_account exists in the database.
268 if ($stmt->num_rows > 0) {
269 $stmt->bind_result($id, $password);
270 $stmt->fetch();
271 // st_account exists, now we verify the password.
272 if (password_verify($_POST['password'], $password)) {
273 // Verification success! User has loggedin!
274 $_SESSION['loggedin'] = TRUE;
275 $_SESSION['name'] = $_POST['username'];
276 $_SESSION['id'] = $id;
277 include_once 'homepage.php';
278 // echo 'Welcome ' . $_SESSION['name'] . '!';
279 } else {
280 echo 'Incorrect username and/or password!';
281 }
282 } else {
283 echo 'Incorrect username and/or password!';
284 }
285 $stmt->close();
286} else {
287 echo 'Could not prepare statement!';
288}
289?>
290
291<?php
292// check to see if the user is logged in
293if ($_SESSION['loggedin']) {
294 // user is logged in
295 include_once 'homepage.php';
296} else {
297 // user is not logged in, send the user to the login page
298 header('Location: index.html');
299}
300?>