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_a/
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
103 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
<!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
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;
$sql = "SELECT garageId FROM garages";
$stmt = $dbconn->prepare($sql);
$stmt->execute();
$ids["garages"] = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
$sql = "SELECT ownerId FROM owners";
$stmt = $dbconn->prepare($sql);
$stmt->execute();
$ids["owners"] = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
$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 ($ids["garages"] as $id) echo "<option value=$id>$id</option>"; ?>
</select>
</td>
</tr>
<tr>
<td>Owner:</td>
<td>
<select name="owner" required>
<option value="" disabled selected>Choose owner</option>
<?php foreach ($ids["owners"] as $id) echo "<option value=$id>$id</option>"; ?>
</select>
</td>
</tr>
<tr>
<br>
<td><button type="submit">Add car</button></td>
</tr>
</table>
</form>
</body>
</html>