Comment box Script in php with Reply and approve by admin

For own developing website designers looking for comments display option in every page. In this article add PHP code for comment box script in PHP with Reply option and approve by admin. Every comment submitted by user, it will hole and need approve by admin, even reply comment also. Normally most of websites have auto approve comment code, or script in the form of jQuery, Ajax it will complex.

The Comment box generated in php language, no need jquery, Ajax. This simple comment box with reply answer in php  and comment approve by admin features supported ,following code. Basically If you use of jQuery and AJAX , code very complex for use and working. Every comment must approve or moderate by admin only.

Each comment also support Reply answer either admin or user. In some other website contains comment box  with Reply answer there. But that comment auto submit to page, no moderating or delete or edit option. In this article we clear explain as simple comment box in php with Reply answer and comment approve by admin.

Features of Simple Comment Box with Reply Answer

  • Simple php code uses, no need of jQuery and AJAX code
  • Each comment uniquely identified by url  i.e  each page can maintain separate comments
  • All comments and Reply comments must be approve or moderate by admin only
  • User comments accept or delete or edit comment options available.
  • One MySql table suitable for all works (Comments and Reply comments)
  • One php code for comments display, One for creating comments.
  • Auto focus available for Auto Reply comments.

Comment box Script in php with Reply and approve by admin
Comment box Script in php with Reply and approve by admin


Steps to Creating Comment Box with reply

  1. Creating table (  tbl_comment.sql )
  2. Comment box html code include Insert query ( commentbox.php )
  3. Comments display php code with beautiful model css code inline tag ( commentlist.php )
  4. Admin accept , reject , delete  Comments menu option php code ( permissions.php )
  5. How to use, any page inserting example ( index.php )
  6. Use database setting file ( dbsetting.php )
  7. Use image as comment avatar. To download Click Here

Creating table (  tbl_comment.sql )

The following code as sql create table

CREATE TABLE `tbl_comment` (
  `cid` int(11) NOT NULL AUTO_INCREMENT,
  `pid` int(11) DEFAULT NULL,
  `comment` varchar(200) NOT NULL,
  `name` varchar(50) NOT NULL,
  `email` varchar(100) NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
  'current_url' varchar(200) NOT NULL,
  'moderate' int(2) NOT NULL 0,
  'reply' int(2) NOT NULL 0,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `tbl_comment`
  ADD PRIMARY KEY (`cid`);

In the above sql table, Where

  • cid --- comment id, is used to uniquely identified
  • pid --- parent id , is used to Reply Comment purpose , this comment row display under the main comment
  • comment --- actual comment as maximum 200 character length
  • name --- comment author name
  • email --- author email
  • date --- Current date and time
  • current_url --- display comment only that particular page only
  • moderate --- gives either 0 or 1
    • 0 -- No moderate or comment not approve by admin
    • 1 -- moderated or comment approved by admin
  • reply --- gives either 0 or 1
    • 0 -- It is main comment , no reply comments
    • 1 -- It is also main comment, but have some reply comments under of this

Comment box html code include Insert query ( commentbox.php )

The following php code for creating comment asking box and Mysql Insert query within

<?php
include_once 'dbsetting.php';

$base_url = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'];
$link = $base_url . $_SERVER["REQUEST_URI"];

if(isset($_REQUEST['replybutton']))
{
$rpid=$_POST['pid1'];
}

if(isset($_REQUEST['submitform']))
{
$result=executeQuery("select max(cid) as std from tbl_comment");
$r=mysql_fetch_array($result);
if(is_null($r['std']))
$newstd=1;
else
$newstd=$r['std']+1;
if($_POST['rrpid'])
{
$newpid=$_POST['rrpid'];
}
else
{
$newpid=$newstd;
}
$comment=trim($_POST['comment']);
$name=trim($_POST['cname']);
$email=trim($_POST['cemail']);

$query="insert into tbl_comment values($newstd,$newpid,'".$comment."','".$name."','".$email."',NOW(),'".$link."',0,0)";

if(executeQuery($query))
{
$s=1;
}
else
{
echo "<h3 style='color:red;'>".mysql_error()."</h3>";
}
if($s)
{
$sql="UPDATE tbl_comment SET reply = 1 WHERE cid = ".$_POST['rrpid'].";";
if(executeQuery($sql))
{
$rs=1;
}

}
}
?>
<br/>
<?php
if(isset($_REQUEST['replybutton']))
{
echo "<h2 style='red'>Please Fill the following form</h2>";
}

?>
<form action="<?php echo $link; ?>" method="post" id="commentform" name="commentform">
<table border="0" style="background-color:#CCCCCC;">
<caption style="background-color:#0099FF; colo:white;" >
<?php
if(isset($_REQUEST['replybutton']))
{
echo "Reply a Comment ";
}
else
{
echo "Leave a Comment ";
}
?>
</caption>
<tr>
<td>Your name <font color="red" > *</font></tdt>
<td>
<?php
if(isset($_REQUEST['replybutton']))
{
?>
<input type="text" name="cname" id="comment_author" value="" size ="50" required="required" placeholder="Name should be 3 to 40 chars" autofocus>
<input type="hidden" name="rrpid" value="<?php echo $rpid; ?>" />
<?php
}
else
{
echo '<input type="text" name="cname" id="comment_author" value="" size ="50" required="required" placeholder="Name should be 3 to 40 chars" >';
}
?>

</td></tr>
<tr>
<td>Your email <font color="red"> *</font></td>
<td><input type="email" name="cemail" value="" size ="50" required="required" placeholder="email contains proper manner" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" title="Please Enter correct format of email"></td></tr>
<tr>
<td>Website :</td>
<td><input type="text" name="cwebsite" value="" size ="50" ></td></tr>
<tr>
<td>Your message <font color="red" size="5"> *</font></td>
<td><textarea name="comment" id="comment" rows="4" cols="50" required="required" placeholder="at least one word"></textarea>
</td></tr>
<tr><td>&nbsp;</td>
<td><input name="submitform" type="submit" value="Submit Comment" /></td>
</tr></table>
</form>
<div style="color:Green;">
<?php
if($s==1)
{
echo "Successful added your comment., but wait some time for moderating your comment.";
}
if(rs==1)
{
echo "Successful added your Reply comment., but wait some time for moderating your comment.";
}
?>
</div>

The output of above code as

Comments display php code  ( commentlist.php )

The following php code to display comments in beautiful manner

<?php

include_once 'dbsetting.php';

$base_url = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'];
$link = $base_url . $_SERVER["REQUEST_URI"];
$result=executeQuery("SELECT * FROM tbl_comment;");

if (mysql_num_rows($result) > 0)
{
?>
<div style="background-color:#FFFFCC; padding:20px; width:600px;">
<?php
while ($r = mysql_fetch_array($result))
{
if(strcmp($link,$r['current_url'])==0)
{
if( ($r['cid'] == $r['pid']) && ($r['moderate']==1) )
{
// Comment main display
// add Reply button
?>
<table border="0" style="background-color:#CCCCFF; width:100%">
<tr>
<td width="10%"><img src="../commentbox/avatar.jpg" width="80px" height="80px" /></td>
<td width="90%">
<table border="0" width="100%">
<caption style="background-color:#3300FF; font-size:14px; color:#FFFFFF;">
<span style="float:left"><?php echo $r['name']; ?></span><span style="float:right"><?php echo $r['date']; ?></span>
</caption>
<tr>
<td style="font-size:12px; text-align:left; text-decoration:none;"><?php echo $r['comment']; ?></td>
</tr>
<tr>
<td>
<form action="<?php echo $link; ?>" method="post">
<input type="hidden" name="pid1" value="<?php echo $r['cid']; ?>" />
<span style="float:right"> <input type="submit" value="REPLY" name="replybutton" >
</form>
</td>
</tr>
</table>
</td>
</tr>
</table>
<br/>
<?php
if( $r['reply']==1)
{
$pid=$r['cid'];
$result1=executeQuery("SELECT * FROM tbl_comment WHERE pid = ".$pid." AND cid != ".$pid.";");
while ($r1 = mysql_fetch_array($result1))
{
// comment display as sub way
// add Reply button
?>
<table border="0" style="background-color:#CCCCFF; width:80%" align="right;">
<tr>
<td width="18%"><img src="../commentbox/avatar.jpg" width="80px" height="80px" /></td>
<td width="82%">
<table border="0" width="100%">
<caption style="background-color:#3300FF; font-size:14px; color:#FFFFFF;">
<span style="float:left"><?php echo $r1['name']; ?></span><span style="float:right"><?php echo $r1['date']; ?></span>
</caption>
<tr>
<td style="font-size:12px; text-align:left; text-decoration:none;"><?php echo $r1['comment']; ?></td>
</tr>

</table>
</td>
</tr>
</table><br/>
<?php
}

}
}
}
}
?>
</div>
<?php
}
?>

The output of above code as


Admin accept , reject , delete  Comments menu option php code ( permissions.php )

By using of following php code , we can perform accept or reject or delete or edit comments.

<?php

include_once 'dbsetting.php';

 if (isset($_REQUEST['delete'])) {
if(executeQuery("delete from tbl_comment where cid=".$_POST['edit'].";"))
{
  $message = "successful Deletion your comment";
}
else
{
$message = mysql_errno();
}

} else if (isset($_REQUEST['savem'])) {

$query = "update tbl_comment set name='".$_REQUEST['name']."', email='".$_REQUEST['email']. "',comment='".$_REQUEST['comment']."',moderate = 1 where cid='".$_POST['edit']."';";
if (!@executeQuery($query))
$message = mysql_error();
else
$message = "User Information is Successfully Updated.";

}

?>
<?php

    echo "<h2>" . $message . "</h2>";

if (isset($_REQUEST['edit'])) {

// To allow Editing comments
$result = executeQuery("select * from tbl_comment where cid = ".$_GET['edit'].";");
if (mysql_num_rows($result) == 0) {
header('Location: permission.php');
} else if ($r = mysql_fetch_array($result)) {

?>
<form action="permission.php" method="post">
<input type="hidden" name="edit" value="<?php echo $_GET['edit']; ?>" />
<table cellpadding="20" cellspacing="20" style="text-align:left;margin-left:15em" >
<tr>
<td>User Name</td>
<td><input type="text" name="name" value="<?php echo $r['name']; ?>" size="16" /></td>

</tr>

<tr>
<td>Email</td>
<td><input type="text" name="email" value="<?php echo $r['email']; ?>" size="16" /></td>

</tr>

<tr>
<td>Comment</td>
<td><textarea name="comment" cols="20" rows="3"><?php echo $r['comment']; ?></textarea></td>
</tr>
<tr>
<td>Current Url</td>
<td><input type="text" name="current_url" value="<?php echo $r['current_url']; ?>" size="16" /></td>
</tr>
</table>
<input type="submit" value="Moderate" name="savem"  >
<input type="submit" value="Cancel" name="cancel" >
<input type="submit" value="Delete" name="delete" >
<?php
$i=0;
}
} else {

$result = executeQuery("select * from tbl_comment where moderate = 0;");
if (mysql_num_rows($result) == 0) {
echo "<h3 style=\"color:#0000cc;text-align:center;\">No Users Yet..!</h3>";
} else {

?>
<table cellpadding="30" cellspacing="10" class="datatable">
<tr>
<th>SNO</th>
<th>User Name</th>
<th>Email-ID</th>
<th>Comment</th>
<th>Edit</th>
</tr>
<?php
while ($r = mysql_fetch_array($result)) {
$i = $i + 1;
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$r['name']."</td><td>".$r['email']."</td><td>".$r['comment']."</td>" ."<td class=\"tddata\"><a title=\"Edit ".$r['cid']."\"href=\"commentmng.php?edit=".$r['cid']."\"><img src=\"../images/edit.png\" height=\"30\" width=\"40\" alt=\"Edit\" /></a></td></tr>";
}
?>
</table>
<?php
}

}

?>

 

How to Use above comments

You can use following php code on any page of your requirements, it works perfectly

<?php
include_once('commentlist.php');
echo "<br/>";
include_once('commentbox.php');
?>

The file ( dbsetting.php )

The following php code as dbsetting.php used to setting  of data base and apply executeQuery( ) userdefined method also

<?php

$dbserver="localhost"; // change as your servername

//username of the MySQL server
$dbusername="sivaramaiah";

//password

$dbpassword="bestvwant";

//database name of the online Examination system
$dbname="bestwant";

$conn=false;

function executeQuery($query)
{
global $conn,$dbserver,$dbname,$dbpassword,$dbusername;
global $message;
if (!($conn = @mysql_connect ($dbserver,$dbusername,$dbpassword)))
$message="Cannot connect to server";
if (!@mysql_select_db ($dbname, $conn))
$message="Cannot select database";

$result=mysql_query($query,$conn);
if(!$result)
$message="Error while executing query.<br/>Mysql Error: ".mysql_error();
else
return $result;

}
function closedb()
{
global $conn;
if(!$conn)
mysql_close($conn);
}
?>

----------------

Php Comment box Script FAQs

Q: Is this php code have AJAX and jQuery script?

A: No, This Comment box generated in php language, no need jquery, Ajax. This simple comment box with reply answer in php and comment approve by admin features supported.


Q: Can you idetifies comments uniquely to each post?

A: Yes , User post any comment, admin can accept or reject and that comment uniquely identifes by post name i.e post url.


Q: How much memory require to use database?

A: In this comment box script use MYSQL database ,in that one tabel , that contains 9 paraneters. Here use less memory.


Q: Is this script run at mobile phone like Android or iphone?

A: Yes, this script run on any device like desktop computers, all mobile phones and tabs also.


Still have Question : Contact Me

0 Comments:

Post a Comment