Webbserverprogrammering 1

Show sourcecode

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

webbserver/Projekt1/

createquiz.php
createtable.php
dbconnection.php
index.php
login.php
myquiz.php
registrera.php
results.php
results2.php
takequiz.php

results.php

36 lines ASCII Windows (CRLF)
<?php
include "dbconnection.php";
    
/** @var PDO $dbconn */
$user_id $_SESSION['user_id'];
$quiz_id = (int)$_POST['quiz_id'];
$score 0;

foreach (
$_POST as $key => $answer_id) {

    if (
strpos($key'question_') === 0) {

        
$stmt $dbconn->prepare(
            
"SELECT is_correct FROM answers WHERE id = :id"
        
);
        
$stmt->execute([':id' => $answer_id]);
        
$answer $stmt->fetch();

        if (
$answer && $answer['is_correct'] == 1) {
            
$score++;
        }
    }
}

$stmt $dbconn->prepare(
    
"INSERT INTO results (user_id, quiz_id, score, created_at)
     VALUES (:user, :quiz, :score, NOW())"
);
$stmt->execute([
    
':user' => $user_id,
    
':quiz' => $quiz_id,
    
':score' => $score
]);

header("Location: results2.php?quiz_id=" $quiz_id);
exit;