Click here to Skip to main content
15,038,345 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
hi,

im trying to make comment box in php.. its working but previous comments was deleted when we type new comment.. but it stores all comments in database.. how to display all comments in the same page plz help mee

here is the code..

XML
<?php
mysql_connect("localhost","root","");
mysql_select_db("cmtdb");
error_reporting(E_ALL ^ E_NOTICE);
$notify = "";
$name=$_POST['name'];
$comment=$_POST['comment'];
$submit=$_POST['submit'];
if(isset($_POST['notify_box'])){ $notify = $_POST['notify_box']; }
$dbLink = mysql_connect("localhost", "root", "");
    mysql_query("SET character_set_client=utf8", $dbLink);
    mysql_query("SET character_set_connection=utf8", $dbLink);

if($submit)
{
if($name&&$comment)
{
$insert=mysql_query("INSERT INTO comment (name,comment) VALUES ('$name','$comment') ");
}
else
{
echo "please fill out all fields";
}
}
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Comment box</title>
</head>
<body>
<center>
<form action="comment.php" method="POST">
<table>
<tr><td>Name: <br><input type="text" name="name"/></td></tr>
<tr><td colspan="2">Comment: </td></tr>
<tr><td colspan="5"><textarea name="comment" rows="10" cols="50"></textarea></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Comment"></td></tr>
</table>
</form>

<?php echo $name;?><br>
<?php echo $comment;?>

<?php
$dbLink = mysql_connect("localhost", "root", "");
mysql_query("SET character_set_results=utf8", $dbLink);
mb_language('uni');
mb_internal_encoding('UTF-8');

$sql="SELECT * FROM comment";
$getquery= mysql_query($sql);
while($row= mysql_fetch_array($getquery))
{
$name=$row['name'];
$comment=$row['comment'];
}
?>

</body>
</html>
Posted
Updated 25-Oct-20 21:27pm
v2

Hello Arunachalam,

Perhaps you could rewrite your page as shown below.
PHP
<?php
mysql_connect("localhost","root","");
mysql_select_db("cmtdb");
error_reporting(E_ALL ^ E_NOTICE);
$notify = "";
$name=$_POST['name'];
$comment=$_POST['comment'];
$submit=$_POST['submit'];
if(isset($_POST['notify_box'])){ $notify = $_POST['notify_box']; }
$dbLink = mysql_connect("localhost", "root", "");
    mysql_query("SET character_set_client=utf8", $dbLink);
    mysql_query("SET character_set_connection=utf8", $dbLink);
 
if($submit)
{
    if($name&&$comment)
    {
        $insert=mysql_query("INSERT INTO comment (name,comment) VALUES ('$name','$comment') ");
    }
    else
    {
        echo "please fill out all fields";
    }
}

$dbLink = mysql_connect("localhost", "root", "");
mysql_query("SET character_set_results=utf8", $dbLink);
mb_language('uni');
mb_internal_encoding('UTF-8');
 
$sql = "SELECT * FROM comment";
$getquery = mysql_query($sql);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Comment box</title>
<style type="text/css">
body { margin: auto 48px; }
</style>
</head>
<body>
    <div>
        <table id="commentTable">
            <colgroup>
                <col width="25%"/>
                <col width="75%"/>
            </colgroup>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Comment</th>
                </tr>
            </thead>
            <tbody>
<?php
while($row = mysql_fetch_array($getquery)) {
    echo '<td>' . $row['name'] . '</td>';
    echo '<td>' . $row['comment'] . '</td>';
}
?>
            </tbody>
        </table>
    </div>
    <form action="comment.php" method="POST">
        <colgroup>
            <col widht="25%" style="vertical-align:top;"/>
            <col widht="75%" style="vertical-align:top;"/>
        </colgroup>
        <table>
            <tr>
                <td><label for="name">Name</label></td>
                <td><input type="text" name="name"/></td>
            </tr>
            <tr>
                <td><label for="comment">Comment:</label></td>
                <td><textarea name="comment" rows="10" cols="50"></textarea></td>
            </tr>
            <tr><td colspan="2"><input type="submit" name="submit" value="Comment"></td></tr>
        </table>
    </form>
</body>
</html>

Regards,
   
v2
Comments
Arun-23 24-May-13 6:48am
   
thanks its working but only name is displaying.. need comment also what could to do for tat..
Prasad Khandekar 24-May-13 6:52am
   
Hello Arunachalam,

I have updated the solution. curly braces '{' were actually missing around the while loop. it should display the comments now.

Regards,
Member 11656941 21-Oct-15 7:32am
   
hello
Member 14840319 22-May-20 4:59am
   
hw
Arun-23 24-May-13 7:02am
   
thank you..
If I were you UI would also add a Captcha image verification to avoid spamming
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900