-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckanswers.php
42 lines (33 loc) · 1.05 KB
/
checkanswers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
require_once("connect.php");
session_start();
if (isset($_POST['answer-submit'])) {
// Checking if our Questions are even attempted
if (!empty($_POST['checkanswer'])) {
// Set a flag for correct answers
$correctAnswers = 0;
$selected = $_POST['checkanswer'];
$sql = "SELECT * FROM questions";
$result = mysqli_query($conn, $sql);
$i = 1;
while ($row = mysqli_fetch_assoc($result)) {
// Matching Database Answerid with User selected answer id
// If ans_id are matched our flag value is updated
if ($row['ans_id'] == $selected[$i]) {
$correctAnswers++;
}
$i++;
}
// Stored our score and attempted question value in session to be used on Result page
$_SESSION['attempted'] = count($_POST['checkanswer']);
$_SESSION['score'] = $correctAnswers;
header("Location: result.php");
exit();
} else {
// If Question not attempted set these variable like this
$_SESSION['attempted'] = 0;
$_SESSION['score'] = 0;
header("Location: result.php");
exit();
}
}