Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg/exercises/sql-intro/sql-intro3/
create-car.php
createtable.php
insertgarage-owner.php
sql-intro3-index.php
sql-intro3a.php
sql-intro3b.php
sql-intro3c.php
sql-intro3d.php
create-car.php
127 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
<!-- insertpost.php -->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Namnlöst dokument</title>
</head>
<body>
<?php
$message = null;
if (isset($_POST['reg']) && isset($_POST['färg']) &&
isset($_POST['garage']) && isset($_POST['ägare']) &&
!empty($_POST['reg']) && !empty($_POST['färg']) &&
!empty($_POST['garage']) && !empty($_POST['ägare'])) {
$regnum = $_POST['reg'];
$färg = $_POST['färg'];
$garage = $_POST['garage'];
$ägare = $_POST['ägare'];
include ('../../../dbconnection.php');
try {
# prepare
$sql = "INSERT INTO car (regnr, color, garage, owner)
VALUES (?, ?, ?, ?)";
$stmt = $dbconn->prepare($sql);
# the data we want to insert
$data = array($regnum, $färg, $garage, $ägare);
# execute width array-parameter
$stmt->execute($data);
echo "New record created successfully";
$lastId = $dbconn->lastInsertId();
echo "id på sista posten: $lastId";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$dbconn = null;
} else {
$message .= "<br />Skriv in!<br /><br />";
}
echo $message;
?>
<?php
include ('../../../dbconnection.php');
try {
$sql = "SELECT * FROM garage";
$stmt = $dbconn->prepare($sql);
$data = array();
$stmt->execute($data);
$garageidlist = array();
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
$garagex = htmlentities($res['garageid']);
array_push($garageidlist, $garagex);
}
$sql = "SELECT * FROM owner";
$stmt = $dbconn->prepare($sql);
$data = array();
$stmt->execute($data);
$ägaridlist = array();
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
$ägarx = htmlentities($res['ownerid']);
array_push($ägaridlist, $ägarx);
}
}
catch(PDOException $e) {
echo $sql . "<br />" . $e->getMessage();
}
?>
<form method="post" action="">
<table>
<tr>
<td>Registreringsnummer:</td>
<td><input type="text" name="reg" size=40 maxlength=100>
</td>
</tr>
<tr>
<td>Färg:</td>
<td><input type="text" name="färg" size=40 maxlength=100></td></tr>
<tr>
<tr>
<td>Garage:</td>
<td>
<select name="garage">
<?php
foreach ($garageidlist as $g){
echo '<option value='.$g.'>'.$g.'</option>';
}
?>
</select>
</td></tr>
<tr>
<tr>
<td>Ägare:</td>
<td>
<select name="ägare">
<?php
foreach ($ägaridlist as $ä){
echo '<option value='.$ä.'>'.$ä.'</option>';
}
?>
</select>
</td></tr>
<tr>
<td>* = obligatoriskt</td>
<td><button type="submit">Lägg till</button></td></tr>
</table>
</form>
</body>
</html>