Show sourcecode
The following files exists in this folder. Click to view.
adminlogin.php
createtables.php
createtest.php
dbconnection.php
index.php
kundsida.php
result.php
test.php
test.php
111 lines UTF-8 Windows (CRLF)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
session_start();
if ($_SESSION["logIn"] == false) {
header("Location: index.php");
}
// print_r($_SESSION["logIn"]);
// print_r($_SESSION["userid"]);
error_reporting(-1); // Report all type of errors
ini_set('display_errors', 1); // Display all errors
ini_set('output_buffering', 0); // Do not buffer outputs, write directly
include('dbconnection.php');
$warningMsg = "";
$testnumber = $_SESSION['testnumber'];
$userid = $_SESSION['userid'];
//se hur många frågor
//lägga till svaret
try {
$sql = "SELECT COUNT(*) FROM questions WHERE testid = $testnumber ";
$res = $dbconn->query($sql);
$count = $res->fetchColumn();
} catch (PDOException $e) {
echo $sql . "<br />" . $e->getMessage();
}
if (isset($_POST["test"])) {
try {
$antalratt = 0;
for ($i = 1; $i <= $count; $i++) {
$correct[] = $_POST[$i];
}
foreach ($correct as $x) {
$sql = "SELECT correct FROM answers where id=$x";
$stmt = $dbconn->prepare($sql);
$data = array();
$stmt->execute($data);
$ratt = $stmt->fetchColumn();
if ($ratt == 1) {
$antalratt++;
}
}
$sql = "INSERT INTO testresults (userid, testid, amountcorrect, test_date)
VALUES ($userid, $testnumber, $antalratt, now())";
// use exec() because no results are returned
$dbconn->exec($sql);
$lastId = $dbconn->lastInsertId();
for ($i = 1; $i <= $count; $i++) {
$answer = $_POST[$i];
$sql = "INSERT INTO useranswers (testresultid, answerid)
VALUES ($lastId, $answer)";
// use exec() because no results are returned
$dbconn->exec($sql);
}
unset($_SESSION['testnumber']);
unset($_POST['test']);
unset($_POST['submit']);
header("Location: kundsida.php");
} catch (PDOException $e) {
echo $sql . "<br />" . $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<nav>
<a href="index.php">Login</a>
<a href="result.php">Resultat</a>
<a href="kundsida.php">Kundsida</a>
<a href="adminlogin.php">Admin</a>
</nav>
<h1>Test <?php $testnumber ?> </h1>
<form action="" method="post">
<?php
try {
$sql = "SELECT questions.question, answers.answer
FROM answers
INNER JOIN questions ON questions.id = answers.questionid
INNER JOIN testinfo ON testinfo.id = questions.testid
WHERE questions.testid = $testnumber ";
$stmt = $dbconn->prepare($sql);
$data = array();
$stmt->execute($data);
$a = 1;
$q = 0;
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
if (($a - 1) % 3 == 0) {
echo $res['question'] . '?<br>';
$q++;
}
echo "<input type='radio' id='$a' name='$q' value='$a' required> ";
echo $res["answer"] . '<br>';
$a++;
}
} catch (PDOException $e) {
echo $sql . "<br />" . $e->getMessage();
}
?>
<input type="submit" name="test">
</form>
</body>
</html>