Managing Authors
Let us begin with authors.php, the file that allowsadministrators to add new authors, and delete and edit existing ones. If you're
comfortable with the idea of multipurpose pages, you may want to place the
code for all of this into the single file, authors.php.
Since the code for this file would be fairly long, I'll use separate files
in my examples to break it up a little.The first thing we'll present to an administrator who needs to manage
authors is a list of all authors currently stored in the database. Code-wise,
this is the same as listing the jokes in the database. Since we'll want to
allow administrators to delete and edit existing authors, you should include
links for these functions next to each author's name. Just like the "Delete
this Joke" links in the challenge at the end of "Publishing MySQL Data on the Web", these links will have the ID of the author attached to them,
so that the target document knows which author the user wishes to edit or
delete. Finally, we shall provide a "Create New Author" link that leads to
a form similar in operation to the "Add a Joke" link we created in "Publishing MySQL Data on the Web".
<!-- authors.php -->
<html>
<head>
<title> Manage Authors </title>
</head>
<body>
<h1>Manage Authors</h1>
<p align="center"><a href=">Create New Author</a>
</p>
<ul>
<?php
$cnx = mysql_connect('localhost','root','mypasswd');
mysql_select_db('jokes');
$authors = @mysql_query('SELECT ID, Name FROM Authors');
if (!$authors) {
die('<p>Error retrieving authors from database!<br />'.
'Error: ' . mysql_error() . '</p>');
}
while ($author = mysql_fetch_array($authors)) {
$id = $author['ID'];
$name = htmlspecialchars($author['Name']);
echo("<li>$name ".
"[<a href=''>Edit</a>|".
"<a href=''>Delete</a>]</li>");
}
?>
</ul>
<p align="center"><a href=">Return to Front Page</a>
</p>
</body>
</html>
The htmlspecialchars function used within the while loop in the
code above may be a little worrisome to you. For the moment, you can simply
ignore it. I'll explain exactly what it does in "Editing Authors" below.