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
createtest.php
104 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
<?php
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');
session_start();
$warningMsg = "";
if ($_SESSION["AlogIn"] == false) {
header("Location: index.php");
}
$questions = $_SESSION["questions"];
$choices = $_SESSION["choices"];
print_r($_POST);
if (isset($_POST["submit"])) {
//testet
try {
$sql = "INSERT INTO testinfo (questions)
VALUES ($questions)";
# prepare
$stmt = $dbconn->prepare($sql);
/*** execute the prepared statement ***/
$stmt->execute();
$testId = $dbconn->lastInsertId();
} catch (PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
//frågorna
for ($i = 1; $i <= $questions; $i++) {
$currentquestionnum = "question" . $i;
$currentquestion = $_POST[$currentquestionnum];
$correctnum = "correctanswer" . $i;
$correct = $_POST[$correctnum];
try {
$sql = "INSERT INTO questions (testid, question) VALUES (:testId, :question)";
$stmt = $dbconn->prepare($sql);
// Bind parameters safely
$stmt->bindParam(':testId', $testId, PDO::PARAM_INT);
$stmt->bindParam(':question', $currentquestion, PDO::PARAM_STR);
// Execute the statement
$stmt->execute();
$lastqid = $dbconn->lastInsertId();
} catch (PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
//svaren
for ($d = 1; $d <= $choices; $d++) {
$currentanswernum = "choice" . $d;
$currentanswer = $_POST[$currentanswernum];
$cc = 0;
if ($correct == $d){
$cc = 1;
}
try {
$sql = "INSERT INTO answers (questionid, testId, answer, correct)
VALUES ($lastqid, $testId, $currentanswer, $cc)";
$stmt = $dbconn->prepare($sql);
/*** execute the prepared statement ***/
$stmt->execute();
} 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>
<form action="" method="post">
<?php
for ($i = 1; $i <= $questions; $i++) {
echo "
<br>
<label for='question$i'>Fråga $i</label>
<input type='text' name='question$i' required>
<br>";
for ($e = 1; $e <= $choices; $e++) {
$answernumber = ($i - 1) * $choices + $e;
echo "
<label for='answer$answernumber'>Svar $e</label>
<input type='text' name='choice$answernumber' required>
<br>";
}
echo "
<label for='correctanswer$i'>Rätt nummer</label>
<input type='number' min='1' max ='$choices' name='correctanswer$i' required>
<br>";
}
?>
<input type="submit" name="submit">
</form>
</body>
</html>