Show sourcecode
The following files exists in this folder. Click to view.
createquiz.php
createtable.php
dbconnection.php
index.php
login.php
myquiz.php
registrera.php
results.php
results2.php
takequiz.php
createquiz.php
103 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
<!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>
<?php
include('dbconnection.php');
/** @var PDO $dbconn */
$step = "quiz";
if (isset($_SESSION['quiz_id'])) {
$step = "question";
}
if (isset($_POST['create_quiz'])) {
$stmt = $dbconn->prepare(
"INSERT INTO quiz (title, user_id)
VALUES (:title, :user_id)"
);
$stmt->execute([
':title' => $_POST['title'],
':user_id' => $_SESSION['user_id']
]);
$_SESSION['quiz_id'] = $dbconn->lastInsertId();
$step = "question";
}
if (isset($_POST['save_question'])) {
$quiz_id = $_SESSION['quiz_id'];
$stmt = $dbconn->prepare(
"INSERT INTO questions (quiz_id, question)
VALUES (:quiz_id, :question)"
);
$stmt->execute([
':quiz_id' => $quiz_id,
':question' => $_POST['question']
]);
$question_id = $dbconn->lastInsertId();
for ($i = 1; $i <= 3; $i++) {
$stmt = $dbconn->prepare(
"INSERT INTO answers (question_id, answer, is_correct)
VALUES (:qid, :answer, :correct)"
);
$stmt->execute([
':qid' => $question_id,
':answer' => $_POST["answer$i"],
':correct' => ($_POST['correct'] == $i ? 1 : 0)
]);
}
if ($_POST['action'] === "done") {
unset($_SESSION['quiz_id']);
header("Location: index.php");
exit;
}
}
?>
<h1>Skapa quiz</h1>
<?php if ($step === "quiz") { ?>
<form method="post">
<label>Quiztitel</label><br>
<input type="text" name="title" required><br><br>
<button name="create_quiz">Skapa quiz</button>
</form>
<?php } ?>
<?php if ($step === "question") { ?>
<form method="post">
<label>Fråga</label><br>
<input type="text" name="question" required><br><br>
<label>Svar</label><br>
<input type="radio" name="correct" value="1" required>
<input type="text" name="answer1" required><br>
<input type="radio" name="correct" value="2">
<input type="text" name="answer2" required><br>
<input type="radio" name="correct" value="3">
<input type="text" name="answer3" required><br><br>
<button name="action" value="add">Lägg till fråga</button>
<button name="action" value="done">Klar</button>
<input type="hidden" name="save_question" value="1">
</form>
<?php } ?>
</body>
</html>