How to make a Comment System for your website
"This page has been created by an amateur with no programming background, for amateurs!"
Hey friends, I am sure there will be quite a few out there just like myself who don't come from a computing background but who are running some really simple personal websites. And I am sure they all want to have some sort of a commenting system running on their website so that they are able to get some sort of feedback from their visitors. And I am sure they all know how they can run a third party script to add a third party comment box to their website. But the thought that they will have to make an account with a third party website, and that their comments will exist on that third party's database is a big deterrent.
Well, don't worry folks, I am here to share with you what I've gathered after reading scores of threads and information about "php", the language used to script and create comment boxes. Without much further ado, here is the information that you need.
You basically need four things to run a comment box:
- Database: Which you have to create on your server. That is, log on to your website server and use MySQL software (Or the SQL software provided to you by your server) and create a database. Then use the phpAdmin software or any other php software provided by your server to create a table to store comments and other related data. If your served didn't provide these to you.. TOO BAD!! Commit suicide! Ok kidding! You will have to download the two softwares and install them onto your server.
- Form: Is the html form that you will provide at the end of your web pages for people to fill in their names and comments and then press submit to send the data.
- Php script to upload comments into database: This script will upload what people will submit using the form into the table in your database, for storage.
- Php script to download comments onto your webpage: This script will retrieve the comments posted on a page and display them on your webpage.
Now the way I've done it, I've added the form and php script for downloading comments into one file which is a better way as then all you need is run this file at the end of your webpages with a simple command and off you go.
Now before I explain the other details, let me share with you the syntax of the two files.
Form and php Script for comment retrieval
| comment |
|---|
|
";
echo "$name "; echo "$date "; ?> |
Php file for saving comments into your database table
The fine details of the project
Before we go any further, here are a few things that I would like to share with you about php:
- When writing in php make sure that you close all the quotes and brackets you use with their opposite number otherwise your php script will generate error and you will get nowhere. So for every ', ", <, (, { and [, make sure you put its' opposite number ', ", >, ) } and ].
- In php a double quote mark " means the end of script unless it is followed by either a dot . or a semi colon to continue it further ; which basically means if you forget one of these in the middle, your script will end at the double quote mark and will generate an error.
- Whenever you put a semi colon ; make sure you leave the next line empty. I read it at one of the sources that sometimes the php skips reading the next line after semi colon. I cannot confirm it for you but it's better to be safe than creating a baby.
- Although ?> sign means the end of script but if you will not close your database with a mysql close () command at the end of the file, your database will not close putting extra load on your server. Once again I cannot confirm it but it doesn't hurt anyways. Just a few words.
- It is always a good idea to add a die("Error..") statements so that if something is wrong with the script you don't have to smash your head into the side wall, monitor or your desk, nor you need a hammer to operate your system.
Rest of the points I will explain as I will give the details of the two files.
Let us first talk about database and table:
You will need to use mysql database software provided by your server (99.99%) or downloaded and installed by you on your server, to create a database which will hold your records for you. The name of the database generally is the name you give but in case you are using MySQL the name will be 'username_database' where username refers to the username you use to log into your server to manage your website and database is the name of the database you created. But don't worry, your MySQL software will show the name of the database and the name of the user of the database to you anyway.
Next you will need to create a user for the database using the same software and give it all the authorities over the database you created. (In php file I will mention two strings which are highly important to protect your database details but in a moment). The name of the user in case you are using MySQL will once again be 'username_dbuser' with the parts of the name once again being your server username and the username for the database that you chose.
With all this done, next thing you will need to do is create a table using the phpMyAdmin software, once again either provided by default by your server (web-host) or installed by you on the server. I would like to mention here that the names of the fields that you use for your table, your website visitors will never know them so you preferably keep them simple like I have used (id, topic, comment, name and date - all in small letters to cut the crap out). As you can see I am no programming guru, so the simple things work for me and simple things is what I recommend.
Now while creating your table make sure you don't tick the NULL box so that it's value remains NOT NULL. The five fields that I have used are the basic requirements for a very simple comment system, simple in technique but not in looks as you can notice in the comment box below. For id and topic select the value INT as input type (Which I guess means integer or number) while VARCHAR for comment and name as they can include any type of character (sexy, bitchy, noble - Nah! I mean text, number or special character). While date should be set on "Timesstamp" which means your server will auto update the date column for you every time a new comment will be added to your database. For value (Or I think Maximum value) tick the A_I (which means Auto Increment) box for id as that will enable your server to auto enumerate the comments your page receives like 1, 2, 3, 4 etc. In other words, one headache less. For topic you can select the maximum value to 100 (which is a lot of characters) and I will explain this when I will explain the form. For name, again 100 characters is more than enough unless you expect an entire family to put their names and surnames in one box when they comment. I don't! So I've left it to a more genuine 100 figure. Comment can be as long as you want it to be. For me 1000 was enough. At the end of all this your table should look like this configuration:
Now we are ready to create and use the form cum comment download file and link the form with the comment saving file (which I call post.php but you can call it honulu.php or scoobydoo.php whatever you like).
Form cum Comment download file:
Now before I explain this file I wish to draw your attention to two very important things. Firstly, these two files are php files with a .php extention. And php files donot work with html websites as such unless you tweak your website setup (which luckily is easier than picking your nose) and then add a small string at the end of your webpage html (which is irritating like girlfriends but is as pleasurable).
First, to tweak your website, if you have apache handlers provided to you by your server you are in for some luck otherwise happy Googling!! Anyway, I guess you have apache handlers software, or you will have to manually add the command lines ".htm server parsed" and ".html server parsed" to your ".htaccess" file on your server. That is, go to file manager, search .htaccess file and edit it. This means that your sever will treat php files as html files. Piece of cake!! Or is it?
But if you are lucky (If you are you should be buying $20 Million Super Lotto ticket!! What are you doing creating a website?) you will have apache handler on your server and you can use that to add handler "server-parsed" to .html and .htm extensions SEPERATELY (Yeah you will have to type the same thing over again twice, unless you prefer copy paste).
Once this has been done, the next step is to create the form.php and post.php files and then add the following string (please remove the spaces between the arrow-heads and string at either end before using it; also, you will have to replace the file name with whatever you call your file) at the end of your html webpage (before you close the body






