How to create comment box in PHP (All language support)

Posted on Posted in PHP

Please watch the video to have a clear knowledge of how to use the code

In this post I will provide you the source code of How to create a comment box in PHP. Beside this, above the source code you can see a video. If you are a beginner of PHP then I strongly recommend you to watch the video. The video will show you step by step guideline of how to use the source code. Hope all of you will enjoy it.

Put this PHP code at the top of your page

<?php
mysql_connect("mysql host name","mysql user name","mysql password");
mysql_select_db("database name");
$name=$_POST['name'];
$comment=$_POST['comment'];
$submit=$_POST['submit'];

$dbLink = mysql_connect("mysql host name", "mysql user name", "mysql password");
	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') ");
echo "<meta HTTP-EQUIV='REFRESH' content='0; url=commentindex.php'>";
}
else
{
echo "please fill out all fields";
}
}
?>

Next, put this HTML code below the PHP code you pasted earlier

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Comment box</title>
</head>

<body>
<center>
<form action="commentindex.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>

Lastly put the code below the HTML code

<?php
$dbLink = mysql_connect("mysql host name", "mysql username", "mysql password");
	mysql_query("SET character_set_results=utf8", $dbLink);
	mb_language('uni');
	mb_internal_encoding('UTF-8');

$getquery=mysql_query("SELECT * FROM comment ORDER BY id DESC");
while($rows=mysql_fetch_assoc($getquery))
{
$id=$rows['id'];
$name=$rows['name'];
$comment=$rows['comment'];
echo $name . '<br/>' . '<br/>' . $comment . '<br/>' . '<br/>' . '<hr size="1"/>'
;}
?>

</body>
</html>

Update

There was a problem with the source code before. When user reload the page the same comment get posted multiple times. I have edited the code by adding a new line after line 16. The added line is highlighted.

About MD Mosleh Uddin

Trying to create an interesting and comfortable environment for the beginners of different programming languages and web design.

142 thoughts on “How to create comment box in PHP (All language support)

      1. Thanks again for this great tutorial sir.can u give as a tutorial an upgrade to this which we can reply and the date and time of comment and reply is shown.hope it will be posted soon?

    1. Assalamualaikum
      an error occurs when I run the above code following your video tutorial
      Error is “Parse error: syntax error, unexpected T_IF in /data/multiserv/users/937056/projects/2158077/www/community.php on line 11”

      Please help

      1. you may have typed it wrong because the db link works fine what u have to do is eneter the correct local host the name and passowrd u sent in ur db and equal it to $dblink and make sure to put a semi colon at the end…if this does not help run it through an online php validator to see if u have any errors

  1. Parse error: syntax error, unexpected ‘$dbLink’ (T_VARIABLE) in C:\xampp\htdocs\series\Comment box.php on line 8

    1. if you have more than 1 page then you need to change the code a little. actually I have plan of making another tutorial on advanced comment box where i will show all the features and how to use the comment box in multiple pages. please keep in touch with us

      1. hey what you should is show how to comment on an actual item and edit it…what u can do is create a login form sign in start a session and show how to upload an item and place, edit delete comments on the item…btw ur search box tutorial worked like a dream thanks

  2. i m getting errors in line 4, 5 and 6..Notice: Undefined index: name
    this notice m getting….plzzz help me out..m doing my project..nd i need to correct errors before 2nd march

  3. when m posting name nd comment its coming correctly.. widout any notice… bt when m opening index page alone at dat tym notices are showing.. nd 1 more thing when m doing referesh after posting comment den same comment is getting posted without posting any comment

    1. please change those line with the code below.

      $name = isset($_POST['name']) ? $_POST['name'] : '';
      $comment = isset($_POST['comment']) ? $_POST['comment'] : '';
      $submit = isset($_POST['submit']) ? $_POST['submit'] : '';

      Hope this will solve your notice problem.

      1. After Doing All The Coding It Shows ”
        Parse error: syntax error, unexpected ‘$_POST’ (T_VARIABLE) in C:\xampp-portable\htdocs\xampp\comment\un\index.php on line 4″……….
        What to do ??
        on line no four my code is

        $name = isset($_POST[‘name’]) ? $_POST[‘name’] : ”;

      2. Dear Mosleh,

        When i add what you suggested everything disappears, but if i don’t add it it keeps repeating the comment. any other ideas please?

        1. You have to use a header function to redirect it to a script….den on the script redirect it back to the page which is wat i did and it solved the problem…remember evertime u refresh it resend the information stored in the buffer…wat u can do is google how to solve refreshing comments duplication.

      3. error is Undefined index: 21,22,23
        kindly plz sir solve this error
        $name=$_POST[‘name’];
        $comment=$_POST[‘comment’];
        $submit=$_POST[‘submit’];

        if(isset($_POST[‘submit’]))
        {
        if(isset($_POST[‘name’]) && isset($_POST[‘comment’]))
        {
        $insert=mysql_query(“INSERT INTO comment (name,comment) VALUES (‘$name’,’$comment’) “);
        echo “”;

        }
        else
        {

        echo”Plz fill Name Or CommentBox”;

        }
        }

    1. I understood your problem. I have plan to make another tutorial on comment box. I will try to fix those problem in my next comment box tutorial. Thanks for your comment.

      1. ডাটাবেস এ বাংলা ফনট সেভ হচ্ছে । কিন্তু পেজ এ শো করতেছে না । প্লিজ হেল্প করেন একটু

  4. i need calendar codings for including in form for showing date of birth..bt i didn’t find codings on ur website…if possible can send me d codings or can u tell me d website so dat i can take from dere…..if possible plzz help me..m not getting from anywhere…nd thank u 4 helping me…

  5. Unfortunately i am getting this error:

    mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/user/public_html/commentindex.php on line 49

  6. This is my line 49 (line 8 on the third code box on this page while($rows=mysql_fetch_assoc($getquery))

    It also does not allow me to post a comment on my page, comment just disappears after I press the”comment” button

  7. It works fine in my test on a kind of outdated webpage except that i don’t seem to be able to figure out how to get the comments. I simply added “comment” at the end of the page. No box shows but if one clicks on it it goes to a seperate page where one can make comments. But the comments don’tt show on the page after I make them, nor do I know where I can find them. Note that it is simply the very last word on the page. I must not know how either to embed the box itself in the page or to see the results somewhere else.
    This old page is not where I want to use it but it was easiest to me to place it here.
    I am using Dreamweaver CS6 and my webhost is ixwebhosting.com

  8. Hey, i’m trying to add this to my webpage but i’m getting an error with mysql. I’ll think everything it correct. I’ve used mysql before about 1 year ago but forgotten abit so it may be my mistake. I’ll post it below. Thanks!

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a4286996/public_html/commentindex.php on line 49

    Line 49:while($rows=mysql_fetch_assoc($getquery))

  9. I am getting error message

    Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in D:\Hosting\11089830\html\commentindex.php on line 47

  10. hello thanks for the code it works on me but i have a little problem the problem is when i testing to my site it works but when you refresh the page the output of the comment become double.. how can i fix this??? help pls..

  11. I Need Admin Approval Cooment Box in Php which admin approve comment after display user side in php plz post the code

  12. Hi MD Mosleh Uddin and thank you very much for taking the time to make this tutorial, i have the same problem as “neha” with lines 4,5,6, everything works fine and the data is being sent to the database as well as outputting the results but i am getting this message when loading the page.

    Notice: Undefined index: name in C:\xampp\htdocs\xampp\site\comment.php on line 4

    Notice: Undefined index: comment in C:\xampp\htdocs\xampp\site\comment.php on line 5

    Notice: Undefined index: submit in C:\xampp\htdocs\xampp\site\comment.php on line 6

    Thanks again and any help would be much appreciated :)

  13. sir help me .the code that i copied in functionable exept the following:

    Notice: Undefined index: name in C:\xampp\htdocs\commentindex.php on line 23

    Notice: Undefined index: comment in C:\xampp\htdocs\commentindex.php on line 24

    Notice: Undefined index: submit in C:\xampp\htdocs\commentindex.php on line 25

    i tried to organize but still not ok almost the same the codes why is this happen

  14. i have error in line
    Parse error: syntax error, unexpected T_VARIABLE in C:\AppServ\www\……\……\commentindex.php on line 8
    plz help

  15. ামার ডাটাবেস এ বাংলা ফনট সেভ হচ্ছে । কিন্তু পেজ এ শো করতেছে না । প্লিজ হেল্প করেন একটু

  16. i use this code and it working correctly but when i submit the comments goes to another page in which only comments showing i want that when i will submit the comment will post on that page and don’t open another page

  17. im having trubbles with
    Parse error: syntax error, unexpected ‘if’ (T_IF) in C:\xampp\htdocs\commentindex.php on line 12
    and i have no idea how to fix this

  18. Hey when I test the website in browser the following message show up. Once the comment is entered it goes away. Any solution for this message to not show up in the first place. Any help would be really appreciated. Thank You.

    Notice: Undefined index: name in C:\xampp\htdocs\practice\commentindex.php on line 34

    Notice: Undefined index: wish in C:\xampp\htdocs\practice\commentindex.php on line 35

    Notice: Undefined index: submit in C:\xampp\htdocs\practice\commentindex.php on line 36

    1. that is your setting error. USE THIS>>>>include(‘config.php’);
      if(isset($_POST[‘name’])&&isset($_POST[‘comment’])&&isset($_POST[‘submit’]))
      {
      $name=$_POST[‘name’];
      $comment=$_POST[‘comment’];
      $submit=$_POST[‘submit’];
      }
      if(isset($_POST[‘submit’]))
      {
      if(isset($_POST[‘name’])&&isset($_POST[‘comment’]))
      {
      if(!empty($name)&&(!empty($comment)))
      {
      $insert=mysql_query(“INSERT INTO comments (name,comment) VALUES (‘$name’,’$comment’)”);
      }
      else
      {
      echo ‘please fill all fields’;
      }
      }

      }

      ?>

  19. Will this only work for web pages?

    or can you get this to work in just a HTML document that’s not on a server yet?

    because it’s nice to know how everything works and that it works before moving everything to a server. ya know?

    1. you either did not assign $dblink to equal to connect to the database or either u created it and named it wrong when u were assigning it to $dblink make sure its something like $dblink = mysql_connect (“localhost”, “root”,”password); I may have an error in the line but its something along this line and make sure u replce root and paasword with whatever u assgned

  20. Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/indiaekb/public_html/commentindex.php on line 34

  21. when i am posting name and comment its coming correctly.. bit when i doing referesh after posting comment get me same comment is getting posted in page and inserted in datebase … please help me

  22. hey good day!

    your work is very nice. but can i ask a favor? can you please tell how to insert a date and time in it? i tried my own source code but error happens. i am hoping for your immediate response. thnx! :)

  23. This appears to be an extremely insecure way of doing code. It looks very vulnerable to SQL-injection attacks.

    First, you are taking input directly from the user without cleaning it:

    $comment=$_POST[‘comment’];
    $submit=$_POST[‘submit’];

    Then, you are sending that to the SQL server directly without verifying or cleaning it:

    $insert=mysql_query(“INSERT INTO comment (name,comment) VALUES (‘$name’,’$comment’) “);

    If I make my comment have exactly this value:

    ‘); DROP comment; SELECT ‘

    …then your entire history of comments could be deleted.

    1. in there i am getting error of boolean. it says Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in

      can you help me with that please.

  24. hey dear how i can insert photo with its corresponding data in to mysql database like name, idno serial_no, with other attribute
    thank you

  25. when i run it come with this error under a comment box

    ‘ . ‘
    ‘ . $comment . ‘
    ‘ . ‘
    ‘ . ” ;} ?>

  26. Undefined index: name in C:\xampp\htdocs\index.php on line 4

    Undefined index: name in C:\xampp\htdocs\index.php on line 5

    Undefined index: name in C:\xampp\htdocs\index.php on line 6

  27. hi i have a little bit of problem.. i tried the same thing yet something went wrong.. everything is fine no error but when i started to test it. theres no comments appearing and it redirected it to the same page address but the comment box is gone so weird..

  28. Hello Sir !

    I have some problem regarding some special text!
    I use ‘(apostrophe) for example : I’m Cool,
    when i write this comment in your commentbox then it will not add it into your data .. plzz tackle this problem!
    I’m still waiting for your reply !

  29. Thanks for the tutorial,had a problem with this always reverted to facebook comment box.HOWEVER MY ISSUE is in wamp database comment is it a char or what equiv like username is char and max of 30 characters,appreciate if mail the info to my address basilo78@yahoo.com .TIA

  30. Assalamualaikum
    an error occurs when I run the above code following your video tutorial
    Object not found!

    The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

    If you think this is a server error, please contact the webmaster.

  31. I need to know the solution of the error in line NO 4,5,6

    $name=$_POST[‘name’];
    $comment=$_POST[‘comment’];
    $submit=$_POST[‘submit’];

    Error message:Notice: Undefined index: name in C:\wamp\www\browser\commentindex.php on line 4

    Though i have created same field with same name in database

  32. Hi this is a great code but I am new to php I have done everything the video shows and it works great with the excempture the it puts the comments in the commentsibdex.php page instead of the comments page I created in html5.
    Can you please help me

  33. the code works great but the comments are place in the commentindex.php page instead of the comment html page.
    can you please help me with this small issue.
    I am a beginer in php

  34. it showing me error undefined index
    $name=$_POST[‘name’];
    $comment=$_POST[‘comment’];
    $submit=$_POST[‘submit’];

  35. I try to solve it
    by this cod and it worked , l like to share it

    $name = isset($_POST[‘name’]) ? $_POST[‘name’] : ”;

    $comment = isset($_POST[‘comment’]) ? $_POST[‘comment’] : ”;
    $submit = isset($_POST[‘submit’]) ? $_POST[‘submit’] : ”;

  36. all the comments appear at the same time without minding the article the person comment on.
    how can only the comments for a particular article appear?
    my own all comments of different article appear at once

  37. I’ve tried another code on this but whenever it’s refreshed, it fetches the last input which results to repeating the last comment. I’ll try this hoping i dont meet the same problem.

  38. i dont have xampp local server, i have tried it on my hosting server. created an database (commentbox) created table (commenttable) and done everything same as shown in above tutorial video but still it does not shows any comments on my page…

  39. Didnt work for me. – My MySQL Database was not same as you shown in your video. So I got so many following errors.
    Can you send me the database ?

    I got the following errors :

    Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/u447128031/public_html/commentindex.php on line 2

    Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/u447128031/public_html/commentindex.php on line 8

    Warning: mysql_connect(): Access denied for user ‘u447128031_user’@’10.1.2.63’ (using password: YES) in /home/u447128031/public_html/commentindex.php on line 8

    Warning: mysql_query() expects parameter 2 to be resource, boolean given in /home/u447128031/public_html/commentindex.php on line 9

Leave a Reply

Your email address will not be published. Required fields are marked *

19 − seven =