The following files exists in this folder. Click to view.
add.php
216 lines UTF-8 Unix (LF)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
// =============================================================================
//
// add.php
//
// Description: Adds new cars and persons from forms
//
// Author: Johan Hällgren
//
// -----------------------------------------------------------------------------
//
// Requires and includes
//
require_once('dbConn.php');
require_once('common.php');
// -----------------------------------------------------------------------------
//
// Take care of GET-variables
//
$val = (isset($_GET['add']) && $_GET['add']!="") ? $_GET['add'] : "";
// -----------------------------------------------------------------------------
//
// Inserts to the database if any form, car or person, is submitted
//
if($val=="car"){
# Skapa SQL-sats för att lägga in car.
$sql = "INSERT INTO car VALUES (NULL, :personId, :regNr, :make, :model, :year, :price);";
# Förbered SQL-satsen till ett statement
$stmt = $pdo->prepare($sql);
# Skapa en array med parametrar som skall bindas till statement
# Om personId == 0, sätts denna till NULL, annars personId
$prm = array(
'personId' => $_POST['personId']==0 ? NULL : $_POST['personId'],
'regNr' => $_POST['regNr'],
'make' => $_POST['make'],
'model' => $_POST['model'],
'year' => $_POST['year'],
'price' => $_POST['price']
);
# Kör mot databasen
$stmt->execute($prm);
# Om lastInsertId == 0 så har något gått fel.
if($pdo->lastInsertId()==0){
$mess = "Ingen bil lades till.";
} else {
$mess = "En bil är tillagd med id: ".$pdo->lastInsertId().".";
}
} else if ($val=="person") {
# Skapa SQL-sats för att lägga in person.
$sql = "INSERT INTO person VALUES (NULL, :firstName, :familyName, :birthDate, :hasLicence);";
# Förbered SQL-satsen till ett statement
$stmt = $pdo->prepare($sql);
# Skapa en array med parametrar som skall bindas till statement
$prm = array(
'firstName' => $_POST['firstName'],
'familyName' => $_POST['familyName'],
'birthDate' => $_POST['birthDate'],
'hasLicence' => isset($_POST['hasLicence']) ? 1 : 0
);
# Kör mot databasen
$stmt->execute($prm);
$mess = "En person är tillagd med id: ".$pdo->lastInsertId().".";
}
// -----------------------------------------------------------------------------
//
// Create selectionbox for owner in car form
//
$owner = <<<EOD
<select for=personId name=personId>
<option value='0' selected>No owner</option>
EOD;
$sql = "SELECT * FROM person;";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($res as $row) {
$owner .= "\n <option value='{$row['personId']}'>{$row['firstName']} {$row['familyName']} </option>";
}
$owner .= "</select>";
// -----------------------------------------------------------------------------
//
// Create form for car
//
$carForm = <<<EOD
<form action="?add=car" method="POST">
<fieldset>
<ol>
<li>
<label for=personId>Owner</label>
{$owner}
</li>
<li>
<label for=regNr>RegNr</label>
<input name=regNr type=text placeholder="registreringsnummer" required autofocus>
</li>
<li>
<label for=make>Make</label>
<input name=make type=text placeholder="Bilmärke" required>
</li>
<li>
<label for=model>Model</label>
<input name=model type=text placeholder="Modell" required>
</li>
<li>
<label for=year>Year</label>
<input name=year type=text placeholder="Åsmodell" required>
</li>
<li>
<label for=price>Price</label>
<input name=price type=text placeholder="Pris" required>
</li>
</ol>
<button type=submit>Add car</button>
</fieldset>
</form>
EOD;
// -----------------------------------------------------------------------------
//
// Create form for person
//
$personForm = <<<EOD
<form action="?add=person" method="POST">
<fieldset>
<ol>
<li>
<label for=firstName>firstName</label>
<input name=firstName type=text placeholder="firstName" required autofocus>
</li>
<li>
<label for=familyName>familyName</label>
<input name=familyName type=text placeholder="familyName" required>
</li>
<li>
<label for=birthDate>birthDate</label>
<input name=birthDate type=date placeholder="birthDate" required>
</li>
<li>
<label for=hasLicence>hasLicence</label>
<input name=hasLicence type=checkbox>
</li>
</ol>
<button type=submit>Add person</button>
</fieldset>
</form>
EOD;
# mess, om mess finns skall det skrivas ut.
$mess = isset($mess) ? "<p class=\"mess\">".$mess."</p>" : "";
// -----------------------------------------------------------------------------
//
// Sätt aside och content
//
$aside2 = "";
$content = $aside2 == "" ? "content_full" : "content";
// ----------------------------------------------------------------------
//
// Content
//
$html = <<< EOD
<article class="{$content}">
<h1>Lägg till post</h1>
{$mess}
<section class="box">
<h2>Car</h2>
{$carForm}
</section>
<section class="box">
<h2>Person</h2>
{$personForm}
</section>
</article>
EOD;
// -----------------------------------------------------------------------------
//
// Create the html-page
//
$title = "Car";
$charset = "UTF-8";
$style = "stilmall.css";
// Här sköts sidbygget och utskriften.
include_once("page.php");
?>