Källkod
Följande filer och mappar finns under mappen webbserverprogrammering.
Mappar visas till vänster och filer till höger. Klicka på en fil eller mapp för att öppna nedan eller visa dess innehåll.
webbserverprogrammering/exercises/mysql/exercise_3/exercise_3_d/
7 filer
back_button.php
dbconnection.php
index.php
insert_values.php
print_table.php
reset.php
reset_garages_owners.php
dbconnection.php
index.php
insert_values.php
print_table.php
reset.php
reset_garages_owners.php
insert_values.php
119 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
<!DOCTYPE html>
<html lang="sv">
<head>
<title>Add data</title>
<meta charset="utf-8">
<style type="text/css">
select:required:invalid {
color: #666;
}
option {
color: #000;
}
option[value=""][disabled] {
display: none;
}
</style>
</head>
<body>
<?php
function dump($dump) {
echo "<pre>";
var_dump($dump);
echo "</pre>";
}
include('back_button.php');
include ('dbconnection.php');
$message = "";
if (isset($_POST['registrationNmbr']) && isset($_POST['color']) && isset($_POST['garage']) && isset($_POST['owner'])) {
$registrationNmbr = $_POST['registrationNmbr'];
$color = $_POST['color'];
$garage = $_POST['garage'];
$owner = $_POST['owner'];
try {
$sql = "INSERT INTO cars (registrationNmbr, color, garage, owner) VALUES (?, ?, ?, ?)";
$stmt = $dbconn->prepare($sql);
$data = [$registrationNmbr, $color, $garage, $owner];
$stmt->execute($data);
echo "<h3>Car added.</h3>";
}
catch(PDOException $e) {
echo "<hr>" . $sql . "<hr>" . $e->getMessage() . "<hr>";
}
} else {
// $message = "<h3>Skriv in uppgifter nedan för att lägga till en ny användare:</h3>";
}
echo $message;
try {
$sql = "SELECT * FROM garages";
$stmt = $dbconn->prepare($sql);
$stmt->execute();
$garages = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($garages as $field) { foreach ($field as $prop) { $prop = htmlentities($prop); } }
$sql = "SELECT * FROM owners";
$stmt = $dbconn->prepare($sql);
$stmt->execute();
$owners = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($garages as $field) { foreach ($field as $prop) { $prop = htmlentities($prop); } }
} catch(PDOException $e) {
echo "<hr>" . $sql . "<hr>" . $e->getMessage() . "<hr>";
}
// dump($garages);
// dump($owners);
$dbconn = null;
?>
<h2>Add a new car</h2>
<form method="post" action="">
<table>
<tr>
<td>Registration number:</td>
<td><input type="text" name="registrationNmbr" size=20 maxlength=100 required></td>
</tr>
<tr>
<td>Color:</td>
<td><input type="text" name="color" size=20 maxlength=100 required></td>
</tr>
<tr>
<td>Garage:</td>
<td>
<select name="garage" required>
<option value="" disabled selected>Choose garage</option>
<?php foreach ($garages as $g) echo "<option value={$g['garageId']}>{$g['name']}</option>"; ?>
</select>
</td>
</tr>
<tr>
<td>Owner:</td>
<td>
<select name="owner" required>
<option value="" disabled selected>Choose owner</option>
<?php foreach ($owners as $o) echo "<option value={$o['ownerId']}>{$o['name']}</option>"; ?>
</select>
</td>
</tr>
<tr>
<br>
<td><button type="submit">Add car</button></td>
</tr>
</table>
</form>
</body>
</html>