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.
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,
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.