(add comments script) Build PHP Database Driven Websites – Tutorial 8
You don’t have to go through the previous tutorials to attend this one but if you need to learn
Previously we had build a dynamic website for an online games website with a CMS. Now we will write a script to add comments to the online games, you can use the script in any PHP application.
Download file 120.rar “start” folder consist of the work done previously and “final” folder consist of the complete work done in this tutorial. Rename the folder “start” to “egames” copy paste at c:/wwwroot/inetpub/. Open internet explorer and type http://localhost/egames to access the application.
Table Structure
Add table ‘comment’ to store comments entered by users
CREATE TABLE `comment` (
`commentid` int(25) NOT NULL auto_increment,
`productid` int(25) default NULL,
`comment` text,
`date` date default NULL,
`email` varchar(255) default NULL,
PRIMARY KEY (`commentid`)
)
product.php
Open product.php, find <!—listing–> and type the following:
savecomment.php
Open savecomment.php and type the following:
That’s done! yeh that was easy wasn’t it? But the story doesn’t end here … What we have so far is a script that enables a user or a website visitor to add comments. Now our CMS should have the ability to view and delete these comments if desired. And that is the end of the story
So below we will two simple scripts to view and delete comments for a particular game OK..
admin/viewproduct.php
Open admin/viewproduct.php type the following after “Detailed Description”:
admin/deletecomment.php
Open admin/deletecomment.php find <!— listing —>type the following:
Note: Click here to study about spam and how to avoid it. Avoid spam like pornographic links etc on commenting, contactus, forums++ pages using anti spam techniques.
Explanation
| Code | Explanation |
| product.php | |
<script language="JavaScript" src="gen_validatorv1.js" type="text/javascript"></script>
|
Including javascript file for form validation
html form for adding comments calling file savecomment.php |
<SCRIPT LANGUAGE="JavaScript"> |
Form validation |
$res=mysql_query("select * from comment where productid='$productid'"); |
Open table ‘comment’ to view comments of a particular product (in this case ‘game’)while loop goes through all rows of the table where productid matches echo displays comment and email |
| savecomment.php | |
<?
$productid=$_POST["productid"]; $email=$_POST["email"]; |
include config.php connects databaseExtract productid, comment, and email from previous file |
insert into comment values (NULL, '$productid', '$comment', '$date', '$email') |
insert new values in the table ‘comment’ |
| admin/viewproduct.php | |
<? |
The code is same like product.php. Open table ‘comment’ to view comments of a particular product (in this case ‘game’).while loop goes through all rows of the table where productid matches echo displays comment and email |
| admin/deletecomment.php | |
$commentid=$_REQUEST["commentid"]; |
Extrace commentid from URL passed previously |
$strquery="Delete from comment where commentid='$commentid' "; |
delete comment where commentid matches |