Show sourcecode
The following files exists in this folder. Click to view.
webbserverprogrammering/exercises/mysqlintro/kompisar/
createtable.php
dbconnection.php
deletepost.php
deletetable.php
index.html
insertdefaultposts.php
insertpost.php
selectposts.php
updatepost.php
updatepost.php
158 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
<!-- updatepost.php -->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Update</title>
</head>
<body>
<?php
include ('dbconnection.php');
$message = null;
$getid = null;
$getfirstname = null;
$getlastname = null;
$getemail = null;
$getphone = null;
if (isset($_POST['firstname']) && isset($_POST['lastname']) &&
isset($_POST['id']) && !empty($_POST['firstname']) &&
!empty($_POST['lastname']) && !empty($_POST['id'])) {
$id = $_POST['id'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = isset($_POST['email']) ? $_POST['email'] : "_";
$phone = isset($_POST['phone']) ? $_POST['phone'] : "_";
try {
# prepare
$sql = "UPDATE pdodemotable SET firstname=?, lastname=?, email=?, phone=?
WHERE id=?";
$stmt = $dbconn->prepare($sql);
# the data we want to insert
$data = array($firstname, $lastname, $email, $phone, $id);
# execute width array-parameter
$stmt->execute($data);
$message .= "<br />Record updated successfully.<br />";
// clear form from info
$_GET['id'] = null;
}
catch(PDOException $e)
{
$message .= $sql . "<br>" . $e->getMessage();
}
} else {
$message .= "<br />Först väljer du en post.
Sen måste du fylla i minst förnamn och efternamn!<br /><br />";
}
if (isset($_GET['id']) && !empty($_GET['id']) ) {
$id = $_GET['id'];
try {
# prepare
$sql = "SELECT * FROM pdodemotable WHERE id=?";
$stmt = $dbconn->prepare($sql);
# the data we want to insert
$data = array($id);
# execute width array-parameter
$stmt->execute($data);
$res = $stmt->fetch(PDO::FETCH_ASSOC);
$getid = htmlentities($res['id']);
$getfirstname = htmlentities($res['firstname']);
$getlastname = htmlentities($res['lastname']);
$getemail = htmlentities($res['email']);
$getphone = htmlentities($res['phone']);
$message .= "<br />Record was selected successfully.<br />";
}
catch(PDOException $e)
{
$message .= $sql . "<br>" . $e->getMessage();
}
} else {
$message .= "<br />Välj en ny post att uppdatera.<br /><br />";
}
echo $message;
?>
<form method="post" action="">
<table>
<tr>
<td>Förnamn*:</td>
<td>
<input type="text" name="firstname" size="40" maxlength="40"
value="<?= $getfirstname; ?>">
</td>
</tr>
<tr>
<td>Efternamn*:</td>
<td>
<input type="text" name="lastname" size="40" maxlength="40"
value="<?= $getlastname; ?>">
</td>
</tr>
<tr>
<td>Epost:</td>
<td>
<input type="text" name="email" size="20" maxlength="30"
value="<?= $getemail; ?>">
</td>
</tr>
<tr>
<td>Telefon:</td>
<td>
<input type="text" name="phone" size="20" maxlength="10"
value="<?= $getphone; ?>">
</td>
</tr>
<tr>
<td>* = obligatoriskt</td>
<td>
<button type="submit">Lägg till</button>
<input type="hidden" name="id" value="<?= $getid; ?>">
</td>
</tr>
</table>
</form>
<?php
// 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']);
$email = htmlentities($res['email']);
$phone = htmlentities($res['phone']);
$output .= "<tr>".
"<td><a href='?id=$idx'>$idx</a></td>".
"<td>$first</td>".
"<td>$last</td>".
"<td>$email</td>".
"<td>$phone</td>".
"</tr>";
}
$output .= "</table>";
echo "$output";
$dbconn = null;
?>
<button onclick="window.location.href='index.html'">Meny</button>
</body>
</html>