HomePHP Page 5 - Building A Quick-And-Dirty PHP/MySQL Publishing System
Admin Ahoy! - PHP
Looking to quickly add a dynamic news page to your corporate orpersonal Web site? This article shows you how, combining PHP's rapidapplication development capabilities with a MySQL database to create aprimitive news publishing system and administration module in just underfour hours.
At this point in time, I do not really have a simple way to update the database with new information. In order to insert or edit information into the database, I need to know SQL and have access to a MySQL client. This works fine for me, the developer - but remember what the customer said about wanting something simple and easily usable?
Obviously, I cannot ask the customer to learn SQL just to update the database. So I need to develop a simple, friendly interface that she can use to update the database. Which brings me to the second part of the development effort - the administration module.
Based on the functions described to me by the customer, it seems clear (to me, at least) that I will need the following four scripts:
"list.php" - the starting point for the administration module, which lists all press releases currently in the database and allows the administrator to select an individual record for an edit or delete operation;
"edit.php" - the script which allows the administrator to update a selected record;
"delete.php" - the script which allows the administrator to delete the selected record;
"add.php" - the script which allows the administrator to add a new record.
These scripts are stored within the "admin" directory in the source code archive. When the application is finally uploaded to the customer's Web site, this directory will need to be protected against unauthorized usage via Apache's HTTP authentication mechanism (for more information on how this works, take a look at the links at the end of this article).
Let's look at each of these in turn.{mospagebreak title=A List In Time} First up, "list.php". As described above, it simply displays a list of all press releases currently stored in the database, with links to the scripts to actually edit or delete them. Here goes:
<?
// list.php - display list of all press releases
?>
<!-- page header - snip -->
<?
// includes
include("../conf.php");
include("../functions.php");
// open database connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to
connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// generate and execute query
$query = "SELECT id, slug, timestamp FROM news ORDER BY timestamp DESC";
$result = mysql_query($query) or die ("Error in query: $query. " .
mysql_error());
// if records present
if (mysql_num_rows($result) > 0)
{
// iterate through resultset
// print title with links to edit and delete scripts
while($row = mysql_fetch_object($result))
{
?>
<font size="-1"><b><? echo $row->slug; ?></b> [<? echo
formatDate($row->timestamp); ?>]</font>
<br>
<font size="-2"><a href="edit.php?id=<? echo $row->id; ?>">edit</a>
| <a
href="delete.php?id=<? echo $row->id; ?>">delete</a></font>
<p>
<?
}
}
// if no records present
// display message
else
{
?>
<font size="-1">No press releases currently available</font><p>
<?
}
// close connection
mysql_close($connection);
?>
<font size="-2"><a href="add.php">add new</a></font>
<!-- page footer - snip -->
As you can see, this is almost identical to the code used in
the other "list.php" - and well it should be, since it performs a nearly-identical function. Here's what it looks like:
Pay special attention to the links to "edit.php" and "delete.php" in the script above; you'll see that each of these scripts is passed an additional $id variable, which contains the unique record identifier for that particular item.