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

createtest.php

104 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 (
$_SESSION["AlogIn"] == false) {
    
header("Location: index.php");
}

$questions $_SESSION["questions"];
$choices $_SESSION["choices"];
print_r($_POST);

if (isset(
$_POST["submit"])) {
    
//testet
    
try {
        
$sql "INSERT INTO testinfo (questions) 
        VALUES (
$questions)";
        
# prepare
        
$stmt $dbconn->prepare($sql);
        
/*** execute the prepared statement ***/
        
$stmt->execute();
        
$testId $dbconn->lastInsertId();
    } catch (
PDOException $e) {
        echo 
$sql "<br>" $e->getMessage();
    }
    
//frågorna
    
for ($i 1$i <= $questions$i++) {
        
$currentquestionnum "question" $i;
        
$currentquestion $_POST[$currentquestionnum];
        
$correctnum "correctanswer" $i;
        
$correct $_POST[$correctnum];
        try {
            
$sql "INSERT INTO questions (testid, question) VALUES (:testId, :question)";
            
$stmt $dbconn->prepare($sql);
            
            
// Bind parameters safely
            
$stmt->bindParam(':testId'$testIdPDO::PARAM_INT);
            
$stmt->bindParam(':question'$currentquestionPDO::PARAM_STR);
            
            
// Execute the statement
            
$stmt->execute();
            
$lastqid $dbconn->lastInsertId();
        } catch (
PDOException $e) {
            echo 
$sql "<br>" $e->getMessage();
        }
        
//svaren
        
for ($d 1$d <= $choices$d++) {
            
$currentanswernum "choice" $d;
            
$currentanswer $_POST[$currentanswernum];
            
$cc 0;
            if (
$correct == $d){
                
$cc 1;
            }
            try {
            
$sql "INSERT INTO answers (questionid, testId, answer, correct) 
            VALUES (
$lastqid$testId$currentanswer$cc)";
            
$stmt $dbconn->prepare($sql);
            
/*** execute the prepared statement ***/
            
$stmt->execute();
        } 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>
    <form action="" method="post">
        <?php
        
for ($i 1$i <= $questions$i++) {
            echo 
"
            <br>
            <label for='question
$i'>Fråga $i</label>
        <input type='text' name='question
$i' required>
        <br>"
;
            for (
$e 1$e <= $choices$e++) {
                
$answernumber =  ($i 1) * $choices $e;
                echo 
"
        <label for='answer
$answernumber'>Svar $e</label>
        <input type='text' name='choice
$answernumber' required>
        <br>"
;
            }
            echo 
"
        <label for='correctanswer
$i'>Rätt nummer</label>
        <input type='number' min='1' max ='
$choices' name='correctanswer$i' required>
        <br>"
;
        }
        
?>
        <input type="submit" name="submit">
    </form>
</body>
</html>