Speak Now, Or Forever Hold Your Peace - Administration
You've seen how many Web sites (including this one) allow you topost comments and opinions on the material they publish. If you've everwished you had this capability on your Web site, or are simply curious asto how this is accomplished, read on.
Next, I'm going to construct a form which will ask for some basic user information, and allow the user to enter a comment on the article. Once that form is submitted, a PHP script will process the information entered into it and store the result in the database via an INSERT operation.
For purposes of convenience, both the initial form and the result page have been embedded in the same PHP script, "post.php", with the $submit variable used to decide which page is displayed. Here's the initial form.
Once the form is submitted, the same script is called upon to process the data.
// form has been submitted
// process form data
$connection = mysql_connect($hostname, $user, $pass) or die ("Unable to
// indicates new comment, not reply
$replytopost = 0;
// correction for empty subject
// all form fields should be checked before executing the query
$subject = "No subject";
// correction for empty username
$username = "Anonymous";
$query = "INSERT INTO $table (section, article, subject, post, username,
email, replytopost, timestamp) VALUES ('$section', '$article', '$subject',
'$post', '$username', '$email', '$replytopost', NOW())";
$result = mysql_db_query($database, $query, $connection) or die ("Error in
query: $query . " . mysql_error());
<font face="Verdana, Arial" size="2" color="Black">
Your comment has been posted. Click to <a href="list.php?section=<? echo
$section; ?>&article=<? echo $article; ?>">read more comments</a>.
// display initial form
This script simply checks the form variables, corrects empty ones, and
then INSERTs the data into the database. If this is a "new" comment (as opposed to a "reply"), the $replytopost variable is set to 0.
In case you're wondering what the include() is all about at the top of the script, the file "config.php" simply stores some basic parameters required to connect to the database. Here it is.
// database parameters
// alter this as per your configuration
$table = "comments";
$user = "root";
$pass = "gsfd3k6";
$hostname = "localhost";
Once the data has been stored in the database, a success code is
returned, and a link offered to read other comments that may have been posted. Let's take a look at that next.
This article copyright Melonfire 2001. All rights reserved.