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
5 Comments to

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

  1. On November 21st, 2010 at 2:29 am Dave Says:

    Sumaiya,
    I have really enjoyed your series on building a db driven site. I have one question that maybe you can help me with. To build a website that has many products (100’s to 1000’s), the building of category and individual product pages would be labor intensive as well as time consuming. Is there an easier way to accomplish this?

  2. On January 6th, 2011 at 5:44 am admin Says:

    You can either import the data from an excel sheet (.csv format) or use a for loop to enter the data in the database directly but ofcourse you have to type in the category and product details.

  3. On February 17th, 2011 at 9:09 am stanha Says:

    It is really nice series, but i found a problem while adding product, produc.php file don’t really working in my system and gives the error like . “No data inserted”. Please help me..

    Thanks

  4. On February 25th, 2011 at 12:32 am kaden hamilton Says:

    I also had a problem with adding product. Any suggestions?
    KH

  5. On May 5th, 2011 at 6:38 am mindroots Says:

    HI
    NIce Post
    Thanks for sharing the post

Email will not be published

Website example

Your Comment:

 

12,829 spam comments
blocked by
Akismet