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/dbintrotest/
10 filer
create_table.php
database_handler.php
dbconnection.php
dbintrotest.php
delete_row.php
delete_table.php
insert_default_values.php
insert_values.php
print_table.php
update_values.php
database_handler.php
dbconnection.php
dbintrotest.php
delete_row.php
delete_table.php
insert_default_values.php
insert_values.php
print_table.php
update_values.php
delete_row.php
73 lines ASCII Windows (CRLF)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
<!DOCTYPE html>
<html lang="sv">
<head>
<title>Delete row</title>
<meta charset="utf-8">
</head>
<body>
<?php
include ('dbconnection.php');
$message = null;
$id = null;
if (isset($_POST['id']) && !empty($_POST['id'])) {
$id = $_POST['id'];
try {
# prepare
$sql = "DELETE FROM pdodemotable WHERE id=?";
$stmt = $dbconn->prepare($sql);
# the data we want to insert
$data = array($id);
# execute width array-parameter
$stmt->execute($data);
$message .= "<br />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 pdodemotable";
$stmt = $dbconn->prepare($sql);
// fetch width column names, create a table
$data = array();
$stmt->execute($data);
$output = "<table><caption>En ostylad tabell!</caption>";
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
$idx = htmlentities($res['id']);
$first = htmlentities($res['firstname']);
$last = htmlentities($res['lastname']);
$agex = htmlentities($res['age']);
$output .= "<tr>".
"<td>$idx</td>".
"<td>$first</td>".
"<td>$last</td>".
"<td>$agex</td>".
"<td><form method='post' action=''>".
"<input type='hidden' name='id' value='$idx'>".
"<button type='submit'>Ta bort</button></form></td>".
"</tr>";
}
$output .= "</table>";
echo "$output";
$dbconn = null;
?>
</body>
</html>