Building a Comments Script
|Page: 2 of 4
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.
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
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.
| Subject: "Confused"
Date: Mar 03 2008 at 11:33 pm
||I am confused
on where to put
this page. Do
they go into
the html of the
webpage or into
the SQL where
we created the
| Subject: "re: Confused"
Date: Mar 05 2008 at 12:52 am
|At the end of
the article you
can find the
full code to
Each sippet you
see is a part
of the full
You create SQL
the PHP code.
The PHP code to
to reply to
want them to
HTML, the web
must be .PHP
and your server
must have PHP
however. PHP is
free, and 99%
of all hosting
||Rating of 5 (4 Votes)