Saturday, June 2, 2012

Comments php


Blog site like this and some web sites have included place of commenting.It is very important to get ideas from the viewers of pages. For the commenting i have developed fallowing interface.

Add Comments | View Comments

Name:-
Email:-
Comment:-



Bellow scripts create the above comment inter face

<h2><center>Add Comments </center></td></h2>

</table>
<table  align="center"  bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post" action="addComments.php">
<td>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#f2e3cd">
<tr>
<td >Name:-</td>

<td><input name="name" type="text" id="name" size="40" /></td>
</tr>
<tr>
<td>Email:-</td>
<td><input name="email" type="text" id="email" size="40" /></td>
</tr>
<tr>
<td>Comment:-</td>

<td><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<table  align="center" >
<tr>
<td><strong><a href="viewComments.php">View Comments </a> </strong></td>
</tr>
</table>

addComments.php bring the commented data into MySql table. before going to make addComments.php we have to create mysql table called "comments" in your my_database as bellow.

CREATE TABLE `comments` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
`comment` longtext NOT NULL,
`datetime` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Then create the addComments.php as follows.

<?php
$host="localhost";
$username="root";
$password="";
$db_name="my_database";
$tbl_name="comments";
$name=$_POST['name'];
$email=$_POST['email'];
$comment=$_POST['comment'];
$datetime=$_POST['datetime'];
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");

$datetime=date("y-m-d h:i:s"); //date time

$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);

if($result){
echo "Successfully added";
echo "<BR>";

echo "<a href='viewComments.php'>View Your Comments</a>";
}

else {
echo "ERROR";
}

mysql_close();
?>

viewComments.php is as bellow.

<h3><center>View Comments | <a href="comments.html">Add Comments</a> </center></h3>

<?php

$host="localhost";
$username="root";
$password="";
$db_name="my_database";
$tbl_name="comments";

mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="0" align="center" bgcolor="#000000">
<tr bgcolor=#f2e3cd>
<td>ID:-</td>
<td><?php echo $rows['id']; ?></td>
</tr>
<tr bgcolor=#f2e3cd>
<td >Name</td>

<td><?php echo $rows['name']; ?></td>
</tr>
<tr bgcolor=#f2e3cd>
<td>Email:-</td>
<td><?php echo $rows['email']; ?></td>
</tr>
<tr bgcolor=#f2e3cd>
<td>Comment</td>
<td><?php echo $rows['comment']; ?></td>
</tr>
<tr bgcolor=#f2e3cd>
<td >Date and Time :-</td>

<td><?php echo $rows['datetime']; ?></td>
</tr>
</table>

<?php
}
mysql_close(); //close database
?>

No comments:

Post a Comment