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

kundsida.php

122 lines UTF-8 Windows (CRLF)
<?php
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');
session_start();
$warningMsg "";
if (isset(
$_POST["submit"])) {
    
$_SESSION['testnumber'] = $_POST["test"];
    unset(
$_POST['submit']);
    
header("Location: test.php");
}
if (
$_SESSION["logIn"] == false) {
    
header("Location: index.php");
}
if (isset(
$_POST["logout"])) {
    
$_SESSION["userid"] = 0;
    
$_SESSION["logIn"] = false;
    
header("Location: index.php");
}
try {
    
$sql "SELECT COUNT(*) FROM testinfo";
    
$res $dbconn->query($sql);
    
$count $res->fetchColumn();
} 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>
    <style>
        table {
            border: 1px black solid;
        }

        td {
            width: 150px;
        }

        tr {
            width: 100px;
        }

        th {
            width: 100px;
            text-align: center;
            background-color: grey;
        }
    </style>
</head>

<body>
<nav>
        <a href="index.php">Login</a> <br>
        <a href="result.php">Resultat</a> <br>
        <a href="kundsida.php">Kundsida</a> <br>
        <a href="adminlogin.php">Admin</a>
    </nav>
    <header>
        <h1>Välkommen, välj ett test nedan:</h1>
        <form action="" method="post">
            <input type="submit" name="logout" value="Logga ut">
        </form>
    </header>
    <main>
        <form action="" method="post">
            <div>
                <?php
                
for ($i 1$i <= $count$i++) {
                    
// echo "<input type= \"radio \" name = \"test\" value= \"test.$i.\" id= \"test.$i.\"> <label for= \"test.$i.\">HTML</label><br> <br>";
                    
echo "<input type='radio' id='$i' name='test' value='$i'> 
                <label for='
$i'>Test $i</label><br><br>";
                }
                
?>
            </div>
            <input type="submit" name="submit">
        </form>
    </main>
    <footer>
        <h2>Eller visa resultat:</h2>
        <h2>Gjorda tester:</h2>
        <div>
            <?php
            $userid 
$_SESSION['userid'];
            try {
                
$sql "SELECT testid, amountcorrect, test_date FROM testresults WHERE userid = $userid";
                
$stmt $dbconn->prepare($sql);
                
$data = array();
                
$stmt->execute($data);
                
$output "<h1>Tester!</h1><table>
    <tr>
    <th> Test </th>
    <th> Antal Rätt </th> 
    <th> Datum </th> 
    </tr>
    "
;
                while (
$res $stmt->fetch(PDO::FETCH_ASSOC)) {
                    
$output .= "<tr>" .
                        
"<td>" htmlentities($res['testid']) . "</td>" .
                        
"<td>" htmlentities($res['amountcorrect']) . "</td>" .
                        
"<td>" htmlentities($res['test_date']) . "</td>" .
                        
"</tr>";
                }
                
$output .= "</table>";
                echo 
"$output";
            } catch (
PDOException $e) {
                echo 
$sql "<br />" $e->getMessage();
            }

            
$dbconn null;

            
?>
        </div>
    </footer>
</body>

</html>