Webbserverprogrammering 1

Show sourcecode

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

webbsrvprg/exercises/quiz/

adminlogin.php
createtables.php
createtest.php
dbconnection.php
index.php
kundsida.php
result.php
test.php

test.php

111 lines UTF-8 Windows (CRLF)
<?php
session_start
();
if (
$_SESSION["logIn"] == false) {
    
header("Location: index.php");
}
// print_r($_SESSION["logIn"]);
// print_r($_SESSION["userid"]);
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');
$warningMsg "";
$testnumber $_SESSION['testnumber'];
$userid $_SESSION['userid'];

//se hur många frågor
//lägga till svaret
try {
    
$sql "SELECT COUNT(*) FROM questions WHERE testid = $testnumber ";
    
$res $dbconn->query($sql);
    
$count $res->fetchColumn();
} catch (
PDOException $e) {
    echo 
$sql "<br />" $e->getMessage();
}
if (isset(
$_POST["test"])) {
    try {
        
$antalratt  0;
        for (
$i 1$i <= $count$i++) {
            
$correct[] = $_POST[$i];
        }
        foreach (
$correct as $x) {
            
$sql "SELECT correct FROM answers where id=$x";
            
$stmt $dbconn->prepare($sql);
            
$data = array();
            
$stmt->execute($data);
            
$ratt $stmt->fetchColumn();
            if (
$ratt == 1) {
                
$antalratt++;
            }
        }
        
$sql "INSERT INTO testresults (userid, testid, amountcorrect, test_date) 
    VALUES (
$userid$testnumber$antalratt, now())";
        
// use exec() because no results are returned
        
$dbconn->exec($sql);
        
$lastId $dbconn->lastInsertId();
        for (
$i 1$i <= $count$i++) {
            
$answer $_POST[$i];
            
$sql "INSERT INTO useranswers (testresultid, answerid) 
        VALUES (
$lastId$answer)";
            
// use exec() because no results are returned
            
$dbconn->exec($sql);
        }
        unset(
$_SESSION['testnumber']);
        unset(
$_POST['test']);
        unset(
$_POST['submit']);
        
header("Location: kundsida.php");
    } 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>
    <nav>
        <a href="index.php">Login</a>
        <a href="result.php">Resultat</a>
        <a href="kundsida.php">Kundsida</a>
        <a href="adminlogin.php">Admin</a>
    </nav>
    <h1>Test <?php $testnumber ?> </h1>
    <form action="" method="post">

        <?php
        
try {
            
$sql "SELECT questions.question, answers.answer
        FROM answers
        INNER JOIN questions ON questions.id = answers.questionid
        INNER JOIN testinfo ON testinfo.id = questions.testid
        WHERE questions.testid = 
$testnumber ";
            
$stmt $dbconn->prepare($sql);
            
$data = array();
            
$stmt->execute($data);
            
$a 1;
            
$q 0;
            while (
$res $stmt->fetch(PDO::FETCH_ASSOC)) {
                if ((
$a 1) % == 0) {
                    echo 
$res['question'] . '?<br>';
                    
$q++;
                }
                echo 
"<input type='radio' id='$a' name='$q' value='$a' required> ";
                echo 
$res["answer"] . '<br>';
                
$a++;
            }
        } catch (
PDOException $e) {
            echo 
$sql "<br />" $e->getMessage();
        }
        
?>
        <input type="submit" name="test">
    </form>
</body>

</html>