Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg/exercises/databaser/databaser4/
cost.php
createtables.php
databaser4.php
deck.php
deletecost.php
deletedeck.php
deletepitches.php
display.php
pitches.php
pitches.php
137 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Färger</title>
<style>
.list {
color: black;
border: 3px solid black;
}
.list td {
padding: 5px;
}
</style>
</head>
<body>
<?php
include('../../../dbconnection.php'); ?>
<br>
<h1>Lägg till färg</h1>
<?php
if (isset($_POST["action"])) {
$message = null;
if ($_POST["action"] == "Lägg till" &&
isset($_POST['pitch']) && isset($_POST['color']) &&
!empty($_POST['color'])
) {
$pitch = $_POST['pitch'];
$color = $_POST['color'];
try {
# prepare
$sql = "INSERT INTO pitches (pitch, color)
VALUES (?, ?)";
$stmt = $dbconn->prepare($sql);
# the data we want to insert
$data = array($pitch, $color);
# 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 . $e->getMessage();
}
} else {
$message .= "Du måste fylla i informationen!<br /><br />";
}
echo $message;
}
?>
<form method="post" action="">
<table>
<tr>
<td>Pitch:</td>
<td><input type="number" name="pitch" size=5 maxlength=1></td>
</tr>
<tr>
<td>Färg:</td>
<td><input type="text" name="color" size=10 maxlength=10></td>
</tr>
<tr>
<td><input type="submit" name="action" value="Lägg till"></td>
</tr>
</table>
</form>
</body>
<br>
<br>
<h1>Ta bort</h1>
<?php
$message = null;
$pitch = null;
if (isset($_POST["action"])) {
if ($_POST["action"] == "Ta bort") {
$id = $_POST['id'];
try {
# prepare
$sql = "DELETE FROM pitches WHERE id=?";
$stmt = $dbconn->prepare($sql);
# the data we want to insert
$data = array($id);
# execute width array-parameter
$stmt->execute($data);
$message .= "Record deleted successfully.<br />";
} catch (PDOException $e) {
$message .= $sql . "<br>" . $e->getMessage();
}
} else {
$message .= "<br />";
}
echo $message;
}
// Ouput table with all posts
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM pitches";
$stmt = $dbconn->prepare($sql);
// fetch width column names, create a table
$data = array();
$stmt->execute($data);
$output = "<table class='list'>";
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
$id = htmlentities($res['id']);
$pitch = htmlentities($res['pitch']);
$color = htmlentities($res['color']);
$output .= "<tr>" .
"<td>$id</td>" .
"<td>$pitch</td>" .
"<td>$color</td>" .
"<td><form method='post' action=''>" .
"<input type='hidden' name='id' value='$id'>" .
"<input type='submit' name='action' value='Ta bort'></form></td>" .
"</tr>";
}
$output .= "</table>";
echo "$output";
$dbconn = null;
?>
</html>