HomePHP Page 7 - Building A Quick-And-Dirty PHP/MySQL Publishing System
Erasing The Past - 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.
So that takes care of adding new data to the database. Now, how about deleting it?
You'll remember, from the discussion of "list.php" a few pages back, that the script "delete.php" is passed a $id variable, which holds the unique database identifier for the selected news item. The script "delete.php" needs this identifier in order to delete the correct record from the database.
Here's the code that makes up "delete.php":
<?
// delete.php - delete a press release
?>
<!-- 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 = "DELETE FROM news WHERE id = '$id'";
$result = mysql_query($query) or die ("Error in query: $query. " .
mysql_error());
// close database connection
mysql_close($connection);
// print result
echo "<font size=-1>Deletion successful. <a href=list.php>Go back to the
main menu</a>.</font>";
?>
<!-- page footer - snip -->
This is so simple it hardly requires any explanation. The ID
passed to the script via the $id variable is used to construct and execute a DELETE query, which removes the corresponding record from the database. Short, sweet and quite efficient.