Show sourcecode
The following files exists in this folder. Click to view.
adminlogin.php
createtables.php
createtest.php
dbconnection.php
index.php
kundsida.php
result.php
test.php
createtables.php
170 lines UTF-8 Windows (CRLF)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<!-- 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>