Left Quote    Choose a job you love, and you will never have to work a day in your life.
- Confucius    
Right Quote
[login] | [Register]

Building a Comments Script

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

Building a Comments Script


This tutorial will guide you through the process of building a comments script with PHP. The comments script will use a mySQL database to store and retrieve information. Its not too advanced, but additional features can easily be added, such as a delete feature and implementing usernames. If a user is logged in, display their username- if not, display "Guest" or something along those lines. Well discuss that more in detail later. So lets get started.

What you need

In order to build and use this script, you will need hosting that supports PHP, because thats the language we are using to build it with. And access to a mySQL database, because this is the type of database we are using to store and retrieve the comments data. I would recommend using the newest versions of PHP, because I do know some things have changed, like I will use $_POST instead of $_HTTP_POST_VARS, which $_POST the newer (and easier) method.

What to Expect

With this particular script, you can simply include the comments file on every page that you would like comments to be made. Like this site, comments can be made on every page of every article, as well as for every code sample. A person can enter their username, subject of comment, form of contact (email or website) and finally, their comment. When they submit the comment, the page will be refreshed and display their comment. First, will appear the lists of comments for that page. And below that, will be a form to add a comment.

The Setup

The first thing you should do, is build a comments table in your database. You can name the table anything you want. We'll use simply, "comments" for this tutorial. The comments table will have 9 columns: article_id, page, date, time, username, ip, contact, subject, and comment.

  • The article_id is the unique id of the particular article the comment is for. Comment results will not depend on this, but we can use it later for statistical purposes.
  • Page is the url of the article and page number which it appears.
  • Date and time will be when the comment was made.
  • Username will come from the form.
  • IP will contain the posters ip for spam / flood control later on.
  • And finally, contact, subject and comment will also come from whats entered on the form below all comments.

Below is the SQL to create the comments table.

1  |  2  |  3  |  4  |  
Next »


  Subject: "suck on step 1 :(" Date: May 20 2008 at 1:30 pm    
how do i build a comments table in my database? copy and paste the code in notepad or what? do i have to name the file to (.)something later? sorry but iam a TOTAL newbie. please help :)
  Subject: "how do i build a comments table in ..." Date: Nov 08 2008 at 2:30 pm    
Normally when someone says u have to create a table as above, its done most simply by logging into php my admin on your server. Then u open a query window and enter the above code, this will then create a table inside your database.
  Subject: "is this comment system look like th..." Date: Feb 04 2009 at 2:32 pm    
is this comment system look like the one here?

i have a user system on my web site, i would like to add something like this to the site but if one user adds a comment to somone else's listing or pic, i want the user who owns the listing or image to be able to delete the comment from the other user if it they want to.
and also secondly receive an email when there is a comment to the listing or the immage saying that there is a new comment giving the link to it,

would that be possible?

thank you
  Subject: "what about deleting the comment" Date: Feb 06 2009 at 4:04 pm    
can the comment deleted by the page owner?
  Subject: "Sure" Date: Feb 07 2009 at 9:25 pm    

Of course it's possible. Create a delete script that requies variables to be passed to it defining what to delete. Set the permissions appropriately (have the code check that the ID of the message is of the user currently logged in, or a response to their comment). You would need to add a column in the table to identify the ID of the comment it is in response to. Also, only display the delete link if the above described conditions are met.
  Subject: "really works" Date: Jun 14 2009 at 2:11 am    
this was first script I ever got to work -- Thanks!!!!
  Subject: "Wow" Date: Jun 05 2011 at 7:11 am    
I have spent 3 days looking for a script to do this. I am new and trying to build a site from scratch. This is the best tutorial I have ever seen.
  wmm [Guest]
  Subject: "thx" Date: Jul 19 2011 at 2:39 pm    
Thankyou so much for this script... love it.
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