Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg/exercises/databaser/ovn_3/
index.php
lagg_till.php
lista.php
ovn_e.php
lagg_till.php
74 lines UTF-8 Windows (CRLF)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Lägg till bil</title>
</head>
<body>
<?php
if (isset($_GET["regnr"])) {
try {
/** @var PDO $dbconn */
include("../dbconnection.php");
$sql = "INSERT INTO mysqlgrunderovn3car (regnr, color, garage, owner) VALUES(?,?,?,?)";
$statement = $dbconn->prepare($sql);
$data = array($_GET["regnr"], $_GET["color"], $_GET["garage"], $_GET["owner"]);
$statement->execute($data);
echo ('Lade till ' . $_GET["regnr"] . '.<br><br>');
} catch (PDOException $e) {
echo ($e->getMessage());
}
}
?>
<form method="get" action="">
<fieldset>
<legend>Lägg till en bil</legend>
<label for="regnr">regnr</label>
<input type="text" id="regnr" name="regnr" value="abc123" required maxlength="6"><br>
<label for="color">Färg</label>
<input type="text" id="color" name="color" value="orange" required maxlength="30"><br>
<label for="garage">Garage</label>
<select name="garage" id="garage">
<?php
try {
/** @var PDO $dbconn */
include("../dbconnection.php");
$sql = "SELECT * FROM mysqlgrunderovn3garage";
$stmt = $dbconn->query($sql);
while ($info = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo ("<option value=" . $info["garageid"] . ">" . $info["name"] . "</option>");
}
} catch (PDOException $e) {
echo ($e->getMessage());
}
?>
</select>
<br>
<label for="owner">Ägare</label>
<select name="owner" id="owner">
<?php
try {
/** @var PDO $dbconn */
include("../dbconnection.php");
$sql = "SELECT * FROM mysqlgrunderovn3owner";
$stmt = $dbconn->prepare($sql);
$stmt->execute();
while ($info = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo ("<option value=" . $info["ownerid"] . ">" . $info["name"] . "</option>");
}
} catch (PDOException $e) {
echo ($e->getMessage());
}
?>
</select><br>
<button type="submit">Lägg till bil</button>
</fieldset>
</form>
</body>
</html>