November 28, 2014
Hot Topics:

Building Database-Driven Applications with PHP and MySQL: Part II

  • August 25, 2003
  • By Elizabeth Fulghum
  • Send Email »
  • More Articles »

Adding and Editing Articles – article.php

There needs to be actual data in the database before we try to display anything, so a good place to start is with programming the file that will allow new articles to be added (and existing ones edited).

Fundamentally, adding and editing a news article is pretty much the same. The fields in the HTML forms for both processes would be identical, as would any validation on the incoming data. The primary differences between adding and editing occur at two different points:

1. After the form has been submitted, when the information is actually entered into the database
2. In the edit screen, when the form must be pre-filled with the information from the article that is being edited.

Because of these differences, the first inclination might be to create two separate scripts to handle each task, including two separate forms. But by folding the code for both adding and editing articles into a single script and using a single form, chunks of redundant PHP and HTML scripting can be eliminated. The differences outlined above can be handled with relative ease just by using a series of well-placed if statements.

Here is the complete script.

/admin/articles.php:

<?php

//include global file, establishes connection to database
include '../global.php';

//check to see if the form has been submitted
if (isset($_POST['submit'])) {

   //perform any error checking on incoming data
   
   if (!empty($_POST['id'])) {
      //id is set, editing an existing article

      //insert data into database
      mysql_query("UPDATE news SET title='{$_POST['title']}',author='{$_POST['author']}',article='{$_POST['article']}' WHERE id='{$_POST['id']}'");
      $message = 'Article Successfully Edited';
      
   } else {
      //id isn't set, adding new article

      //get today's date, unix timestamp format
      $date=time();

      //insert data into database
      mysql_query("INSERT INTO news (id,title,date,author,article) VALUES('','{$_POST['title']}','$date','{$_POST['author']}','{$_POST['article']}')");
      $message = 'New Article Successfully Added';
   }
}

//get data to prefill form if we're editing an existing entry
if (!empty($_REQUEST['id'])) {
   $result = mysql_query("SELECT id, title, author, article FROM news WHERE id='{$_REQUEST['id']}'");
   list($id,$title,$author,$article)=mysql_fetch_array($result);
}

//output html form
?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Articles</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
td,body {
   font-family: Arial, Helvetica, sans-serif;
   font-size: 12px;
}
-->
</style>
</head>

<body>

<?if (isset($message)) print $message?>
<br><br>

<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<table width="400" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="99">Headline:</td>
<td width="301"><input name="title" type="text" value="<?if (isset($title)) print $title; ?>"></td>
</tr>
<tr>
<td>Author:</td>
<td><input name="author" type="text" value="<?if (isset($author)) print $author; ?>"></td>
</tr>
<tr>
<td>Article:</td>
<td><textarea name="article" wrap="VIRTUAL"><?if (isset($article)) print $article; ?></textarea></td>
</tr>
<tr>
<td> </td>
<td>
   <input type="hidden" name="id" value="<?if (isset($_REQUEST['id'])) print $_REQUEST['id']?>">
   <input type="submit" name="submit" value="submit"></td>
</tr>
</table></form>
</body>
</html>

Let's take a look at each part in detail.

Whether adding or editing an article, the HTML form will always be displayed first, since it is not encapsulated in any If Statements. It can be considered the default state or action of the script.

By placing an If Statement before the form, the execution of the script can easily be diverted, depending on the user's actions. In this case, the If Statement is used to check whether or not the form has been submitted.

If it has, we next need to determine whether an existing article is being edited, or a new one added. When an existing article is being edited, article.php will be called with a link that includes the primary ID number associated with that article. That ID number will be stored in a hidden field in the form (notice that it is referenced through the $_REQUEST array, since the id could be coming from the query string or the form), and passed along with the rest of the data when the form is submitted. To check and see if we're editing an existing article, we need only see if that hidden field is empty or contains a value.

At this point, the script either inserts the new entry into the database, or updates an existing one associated with the id number.

Once complete, a thank you message is generated and displayed. The form is outputted again, ready to add another article or make further changes to the selected one.





Page 2 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel