Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

webbsrvprg/projekt/quiz/

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

quiz.php

92 lines UTF-8 Windows (CRLF)
<?php
session_start
();

// Kontrollera om användaren är inloggad
if (!isset($_SESSION['user_id'])) {
    
header("Location: login.php");
    exit;
}

// Debugging:
// if (!isset($_GET['test_id']) || empty($_GET['test_id'])) {
//     echo "Test-ID saknas.";
//     exit;
// }

$test_id $_GET['test_id'];
include (
'../../incl/dbconnect.php');

try {
    
// Hämta testinformation
    
$stmt $dbconn->prepare("SELECT * FROM testinfo_quiz WHERE id = ?");
    
$stmt->execute([$test_id]);
    
$test $stmt->fetch(PDO::FETCH_ASSOC);

    if (!
$test) {
        echo 
"Testet hittades inte.";
        exit;
    }

    
// Hämta alla frågor för testet
    
$stmt $dbconn->prepare("SELECT * FROM questions_quiz WHERE test_id = ?");
    
$stmt->execute([$test_id]);
    
$questions $stmt->fetchAll(PDO::FETCH_ASSOC);

} catch(
PDOException $e) {
    echo 
"Fel vid hämtning av testdata: " $e->getMessage();
    exit;
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <title><?php echo htmlspecialchars($test['titel']); ?></title>
    <link rel="stylesheet" href="nav.css">

</head>
<body>
<?php include ('nav.php'); ?>

    <h2><?php echo htmlspecialchars($test['titel']); ?></h2>
    <?php if (!empty($test['beskrivning'])): ?>
        <p><?php echo htmlspecialchars($test['beskrivning']); ?></p>
    <?php endif; ?>

    <form method="post" action="submit_quiz.php">
        <?php if (!empty($questions)): ?>
            <?php foreach($questions as $index => $question): ?>
                <div>
                    <p><strong>Fråga <?php echo $index 1?>:</strong> <?php echo htmlspecialchars($question['fråga_text']); ?></p>
                    
                    <?php
                    
// Hämta svarsalternativ för den aktuella frågan
                    
$stmt $dbconn->prepare("SELECT * FROM answers_quiz WHERE fråga_id = ?");
                    
$stmt->execute([$question['id']]);
                    
$answers $stmt->fetchAll(PDO::FETCH_ASSOC);
                    
?>
                    
                    <?php if (!empty($answers)): ?>
                        <?php foreach ($answers as $answer): ?>
                            <div>
                                <label>
                                    <input type="radio" name="answer[<?php echo $question['id']; ?>]" value="<?php echo $answer['id']; ?>" required>
                                    <?php echo htmlspecialchars($answer['svar_text']); ?>
                                </label>
                            </div>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <p>Inga svarsalternativ hittades för denna fråga.</p>
                    <?php endif; ?>
                </div>
                <hr>
            <?php endforeach; ?>
            <input type="hidden" name="test_id" value="<?php echo htmlspecialchars($test_id); ?>">
            <input type="submit" value="Skicka in svar">
        <?php else: ?>
            <p>Inga frågor hittades för detta test.</p>
        <?php endif; ?>
    </form>
</body>
</html>