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

createtables.php

170 lines UTF-8 Windows (CRLF)
<!-- createtable.php -->
<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>Create</title>
</head>

<body>
    <?php
    
include('dbconnection.php');
    try {

        
// sql to create table
        
$sql "CREATE TABLE users (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    name VARCHAR(30) NOT NULL,
    username VARCHAR(30) NOT NULL,
    password VARCHAR(30) NOT NULL,
    login_date DATETIME
    )"
;
        
$dbconn->exec($sql);
        
$sql "INSERT INTO users (name, username, password, login_date) 
        VALUES ('daniel', 'danieleh', 'daniel', now())"
;
        
$stmt $dbconn->prepare($sql);
        
$stmt->execute();
        echo 
"Table created successfully";



        
// sql to create table
        
$sql "CREATE TABLE admins (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    username VARCHAR(30) NOT NULL,
    password VARCHAR(30) NOT NULL
    )"
;
        
$dbconn->exec($sql);
        
$sql "INSERT INTO admins (username, password) 
        VALUES ('admin', 'admin')"
;
        
$stmt $dbconn->prepare($sql);
        
$stmt->execute();
        echo 
"Table created successfully";

        
// sql to create table
        
$sql "CREATE TABLE testinfo (
                    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
                    questions INT(6) NOT NULL
                    )"
;
        
$dbconn->exec($sql);
        
$sql "INSERT INTO testinfo (questions) 
        VALUES (5)"
;
        
$stmt $dbconn->prepare($sql);
        
$stmt->execute();
        echo 
"Table created successfully";


        
// sql to create table
        
$sql "CREATE TABLE questions (
                            id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
                            testid INT(6) UNSIGNED,
                            question VARCHAR(30),
                            FOREIGN KEY (testid) REFERENCES testinfo(id)
                            )"
;
        
$dbconn->exec($sql);
        
$sql "INSERT INTO questions (testid, question) 
    VALUES (1, ?)"
;
        
# prepare
        
$stmt $dbconn->prepare($sql);
        
$data = array('Vad är 1 + 1?');
        
$stmt->execute($data);
        
$data = array('Vad är 1 + 2?');
        
$stmt->execute($data);
        
$data = array('Vad är 2 - 1?');
        
$stmt->execute($data);
        
$data = array('Vad är 3 - 1?');
        
$stmt->execute($data);
        
$data = array('Vad är 4 - 1?');
        
$stmt->execute($data);
        echo 
"Table created successfully";
        
        
        
// sql to create table
        
$sql "CREATE TABLE answers (
                            id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
                            questionid INT(6) UNSIGNED,
                            testid INT(6) UNSIGNED,
                            answer VARCHAR(30),
                            correct INT(1),
                            FOREIGN KEY (questionid) REFERENCES questions(id),
                            FOREIGN KEY (testid) REFERENCES testinfo(id)
                            )"
;
        
$dbconn->exec($sql);
        
$sql "INSERT INTO answers (questionid, testid, answer, correct) 
    VALUES (?, 1, ?, ?)"
;
        
# prepare
        
$stmt $dbconn->prepare($sql);
        
$data = array(1'1'0);
        
$stmt->execute($data);
        
$data = array(1'2'1);
        
$stmt->execute($data);
        
$data = array(1'3'0);
        
$stmt->execute($data);

        
$data = array(2'1'0);
        
$stmt->execute($data);
        
$data = array(2'2'0);
        
$stmt->execute($data);
        
$data = array(2'3'1);
        
$stmt->execute($data);

        
$data = array(3'1'1);
        
$stmt->execute($data);
        
$data = array(3'2'0);
        
$stmt->execute($data);
        
$data = array(3'3'0);
        
$stmt->execute($data);

        
$data = array(4'1'0);
        
$stmt->execute($data);
        
$data = array(4'2'1);
        
$stmt->execute($data);
        
$data = array(4'3'0);
        
$stmt->execute($data);

        
$data = array(5'1'0);
        
$stmt->execute($data);
        
$data = array(5'2'0);
        
$stmt->execute($data);
        
$data = array(5'3'1);
        
$stmt->execute($data);
        
        echo 
"Table created successfully";

        
// sql to create table
        
$sql "CREATE TABLE testresults (
            id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
            userid INT(6) UNSIGNED,
            testid INT(6) UNSIGNED,
            amountcorrect INT(6) UNSIGNED,
            test_date DATETIME,
            FOREIGN KEY (userid) REFERENCES users(id),
            FOREIGN KEY (testid) REFERENCES testinfo(id)
            )"
;
        
$dbconn->exec($sql);
        echo 
"Table created successfully";

        
// sql to create table
        
$sql "CREATE TABLE useranswers (
                    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
                    testresultid INT(6) UNSIGNED,
                    answerid INT(6) UNSIGNED,
                    FOREIGN KEY (testresultid) REFERENCES testresults(id),
                    FOREIGN KEY (answerid) REFERENCES answers(id)
                    )"
;
        
//correct = 1
        //incorrect = 0
        
$dbconn->exec($sql);
        echo 
"Table created successfully";
    } catch (
PDOException $e) {
        echo 
$sql "<br>" $e->getMessage();
    }
    
//Rensa kopplingen till databasen
    
$dbconn null;

    
?>
    <a href="../ovn_sql1.php">Tillbaks till meny</a>
</body>

</html>