Left Quote    A clever person solves a problem. A wise person avoids it.
- Einstein    
Right Quote
[login] | [Register]

Building a Comments Script

by: bs0d
Page: 2 of 4
(View All)

Starting the Code

Now that we have our table all setup, we can now begin writing the code. Since we are first displaying comments, then the form to submit comments- our code will start exactly the same. First, we will query the comments table for any rows existing that are for the url of the page we are currently viewing. We get the page, by the server variable $_SERVER['REQUEST_URI']. This will get the url that appears in their browser. Also, keep in mind that we cannot execute the query until we've connected to the database. So lets look at the code:

The first line of code calls a file that already contains the database connection information. When I do this, a connection is made. Next you see the query, which orders the results in ascending order by the time they were posted. Below all of that is an if statement that will check if the query executed, if not- the error message will be displayed.

Displaying Comments

Up to this point, we have connected to the database, and executed a query to check if any comments existed for the page we are viewing. Now, we will get the number of comments by using mysql_num_rows(); And check if that number is greater than 1. If that condition is true- we display the comments, if not- we display a message indicating that no comments exist. See the code below:

In the code above, I included minimal formatting and no colors. You can do this on your own and may decide to use CSS (recommended) or hard code colors and other formatting directly in the code. Now I would also like to point out a few other things. The while loop will continue to loop until each row is displayed from the query (using mysql_fetch_object();).

Also, since we are displaying users input from the form, we can assume the worst by not trusting the input is valid. Hince, we use the function htmlspecialchars(); to disable any html. And remember to use stripslashes(); as well when displaying the data, because we addslashes(); when inserting the data.

Determine Submission

In between showing the comments (if available) and displaying the form to add a comment- we will now determine if the submit button is pressed to add the comment. And if so, we will go through the process of validating input data, and inserting it into the table. Let us look at some of the code:

This code starts the condition that tests if the submit button is pressed, if so below we are checking if there is content in the input fields of the form. If there is not, the script ends with die(); indicating where the error is located.

1  |  2  |  3  |  4  |  
Next »


  Subject: "Confused" Date: Mar 03 2008 at 11:33 pm    
I am confused on where to put the code snippets that you have provided on this page. Do they go into the html of the webpage or into the SQL where we created the db.
  Subject: "re: Confused" Date: Mar 05 2008 at 12:52 am    

At the end of the article you can find the full code to this script. Each sippet you see is a part of the full script. The article is describing what each snippet does.

You create SQL queries within the PHP code. The PHP code to display comments & associated form to reply to comments should be included wherever you want them to appear within your website.

Remember, although your website is written using HTML, the web page extensions must be .PHP and your server must have PHP installed before the script will execute. This is probably most applicable if you're running your own server, however. PHP is free, and 99% of all hosting companies provide PHP. Good luck!
You Must be logged in or a member to comment.

Tutorial Stats

Tutorial Stats

41 Total Comments
4.75 Rating of 5 (4 Votes)


Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article



· Simple PHP Tutorial
· One File Website
· Pagination with PHP
· Complete Members System
· Variable Scope

"" Copyright © 2002-2021; All rights lefted, all lefts righted.
Privacy Policy