Show sourcecode
The following files exists in this folder. Click to view.
account.php
admin_register.php
create_quiz.php
create_tables.php
index.php
login.php
nav.css
nav.php
quiz.php
quiz_select.php
register.php
result_details.php
result_simple.php
submit_create_quiz.php
submit_quiz.php
submit_quiz.php
70 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
<?php
session_start();
include ('../../incl/dbconnect.php');
// Kontrollera om användaren är inloggad
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit;
}
// Kontrollera om test_id finns i POST-data
if (isset($_POST['test_id']) && !empty($_POST['test_id'])) {
$test_id = $_POST['test_id'];
echo "Debug i submit_quiz.php: test_id är " . htmlspecialchars($test_id) . "<br>";
} else {
die("Fel i submit_quiz.php: test_id saknas!");
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['test_id'])) {
$user_id = $_SESSION['user_id'];
$test_id = $_POST['test_id'];
$answers = $_POST['answer']; // Assoc-array med fråga_id => svar_id
$score = 0;
try {
$dbconn->beginTransaction();
// Spara testresultatet i results_quiz och hämta det nya resultat_id
$stmt = $dbconn->prepare("INSERT INTO results_quiz (kund_id, test_id, poäng) VALUES (?, ?, ?)");
$stmt->execute([$user_id, $test_id, $score]);
$resultat_id = $dbconn->lastInsertId(); // Hämtar ID för det insatta resultatet //Behövde ändra til detta för att annars misslyckas foreign key
// Loopa genom användarens svar och jämför med rätta svaren
foreach ($answers as $question_id => $answer_id) {
$stmt = $dbconn->prepare("SELECT korrekt FROM answers_quiz WHERE id = ?");
$stmt->execute([$answer_id]);
$correct = $stmt->fetchColumn();
// Om svaret är korrekt öka poängen
if ($correct) {
$score++;
}
// Spara användarens svar i user_answers_quiz med rätt resultat_id
$stmt = $dbconn->prepare("INSERT INTO user_answers_quiz (resultat_id, fråga_id, valt_svar_id, korrekt) VALUES (?, ?, ?, ?)");
$stmt->execute([$resultat_id, $question_id, $answer_id, $correct]);
}
// Uppdatera poängen i results_quiz
$stmt = $dbconn->prepare("UPDATE results_quiz SET poäng = ? WHERE id = ?");
$stmt->execute([$score, $resultat_id]);
$dbconn->commit();
// Omdirigera till resultatsidan
header("Location: result_simple.php?test_id=" . $test_id);
exit;
} catch (PDOException $e) {
echo "Fel vid insättning av data: " . $e->getMessage();
exit;
}
} else {
echo "Ogiltig förfrågan.";
exit;
}
?>
<a href="result_simple.php?test_id=<?php echo urlencode($test_id); ?>">gå till resultat simple</a> debugging