This is an old revision of the document!
list-persons.html
<html>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<div id='liclass'>
</div>
<?php
require('db.php');
$dbh = get_db();
$person_list =
$dbh->query('select id, fullname, birthdate, country_code from person')
->fetch_all(MYSQLI_ASSOC);
// echo format_html_table($person_list, 'persontable');
?>
<div id="person_list">
<?php
foreach ($person_list as $person) {
printf (
"<li><a href='update_person.php?id=%d'>%s</a>, %s, %s </li>\n",
$person['id'],
$person['fullname'],
$person['birthdate'],
$person['country_code']
);
}
?>
</div>
<html>
update_person.php
<?php
require('db.php');
$dbh = get_db();
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$id = $_GET['id'];
$country_list =
$dbh->query('select Code, Name from country')
->fetch_all(MYSQLI_ASSOC);
$sth = $dbh->prepare(
'select fullname, birthdate, country_code from person where id = ?'
);
$id = $_GET['id'];
$sth->bind_param("d", $id);
$sth->execute();
$sth->bind_result($fullname, $birthdate, $country_code);
$sth->fetch();
?>
<html>
<form
name="personform"
id="personform"
action="<?php echo $_SERVER['PHP_SELF'];?>"
method="POST">
<input name="id" id="id" type="hidden" value = <?php echo $id?>/>
<p>Fullname: <input name="fullname" id="fullname" type="text" size="30"
value="<?php echo$fullname?>"></p>
<p>Birthdate: <input name="birthdate" id="birthdate" type="text" size="30"
value=<?php echo $birthdate?>
></p>
<p>Country:
<select name="country_code" id="country_code"
>
<?php
foreach ($country_list as $row) {
if ($row['Code'] == $country_code) {
$selected = 'selected';
} else { $selected = ''; }
printf('<option value="%s" %s>%s</option>',
$row['Code'], $selected, $row['Name'] );
}
?>
</select>
</p>
<p><input type="submit" value="Save"></p>
</form>
<?php
}
elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
?>
<meta http-equiv="refresh" content="0; url=list-persons.php" />
<?php
$sth = $dbh->prepare(
"update person
set fullname = ?,
birthdate = ?,
country_code = ?
where id = ?"
);
$fullname = $_POST['fullname'];
$birthdate = $_POST['birthdate'];
$country_code = $_POST['country_code'];
$id = $_POST['id'];
$sth->bind_param("sssd", $fullname, $birthdate, $country_code, $id);
$sth->execute();
}
<h2>Update oder Insert</h2>