PHPJavascript

Web Development Guide – My Personal Library of Tutorials and Scripts

(add comments script) Build PHP Database Driven Websites – Tutorial 8

December25

Download

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>

<form name="myform" action="savecomment.php" method="post">
<input type="hidden" name="productid" value="<?echo $productid?>" />
Email: <input type="text" name="email" size="38" />
Comment: <textarea name="comment" rows="5" cols="30"></textarea>
<input type="submit" name="submit" value="Submit Comment" />
</form>

Including javascript file for form validation

html form for adding comments calling file savecomment.php

<SCRIPT LANGUAGE="JavaScript">
var myformValidator = new Validator("myform");
myformValidator.addValidation("email","req","Please enter the Email");...</SCRIPT>
Form validation
$res=mysql_query("select * from comment where productid='$productid'");
while($row=mysql_fetch_array($res)){
$comment=stripslashes($row["comment"]); $email=$row["email"];
echo $comment." by ".$email."<br>";
}
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
<?
include("config.php");

$productid=$_POST["productid"]; $email=$_POST["email"];
$comment=addslashes($_POST["comment"]); $date=date(’Y/m/d’);

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
<?
$res=mysql_query("select * from comment where productid='$productid'"); $tot=mysql_num_rows($res);
if($tot==0){echo "No comments available"; } else {
while($row=mysql_fetch_array($res)) { $comment=$row["comment"]; $email=$row["email"]; $commentid=$row["commentid"];
echo "$comment by $email <a href='deletecomment.php?commentid=$commentid'><font class='tredb'>DELETE</font></a><br>"; } } ?>
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
Click ‘DELETE’ to call deletecomment.php passing commentid to delete particular comment if desired

admin/deletecomment.php
$commentid=$_REQUEST["commentid"]; Extrace commentid from URL passed previously
$strquery="Delete from comment where commentid='$commentid' ";
mysql_query($strquery) or die("Cannot Delete Record From comment");
delete comment where commentid matches
posted under Development Guide

Email will not be published

Website example

Your Comment:

 

3,048 spam comments
blocked by
Akismet