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

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

mysql_connect("mysql host name","mysql user name","mysql password");
mysql_select_db("database name");

$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);

$insert=mysql_query("INSERT INTO comment (name,comment) VALUES ('$name','$comment') ");
echo "<meta HTTP-EQUIV='REFRESH' content='0; url=commentindex.php'>";
echo "please fill out all fields";

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

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

<form action="commentindex.php" method="POST">
<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>

Lastly put the code below the HTML code

$dbLink = mysql_connect("mysql host name", "mysql username", "mysql password");
	mysql_query("SET character_set_results=utf8", $dbLink);

$getquery=mysql_query("SELECT * FROM comment ORDER BY id DESC");
echo $name . '<br/>' . '<br/>' . $comment . '<br/>' . '<br/>' . '<hr size="1"/>'



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.

142 Responses so far.

  1. shabeerksbr says:

    Its useful…

  2. Jessto says:

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

  3. Jessto says:

    how to fix this problem? tnx

  4. Erik says:

    Testing this commentbox

  5. Stripz says:

    What if you are to use it for more than 1 page

    • MD Mosleh Uddin says:

      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

      • Stripz says:

        Where are you going 2 do that please?

      • faoud says:

        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

  6. neha says:

    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

  7. neha says:

    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

    • MD Mosleh Uddin says:

      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.

      • Navneet Jha says:

        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’] : ”;

      • angelica says:

        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?

        • faoud says:

          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.

      • Nasser says:

        without any Benefiفs .. please help me

      • ahsan says:

        error is Undefined index: 21,22,23
        kindly plz sir solve this error

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


        echo”Plz fill Name Or CommentBox”;


    • sweety says:

      i am facing the problme.plz help me.its urgent.

  8. neha says:

    plzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz…………………………………help me………..

    • MD Mosleh Uddin says:

      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.

      • রাজু says:

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

  9. neha says:

    thanx worked…plzz upload some more videos of php

  10. neha says:

    i need calendar codings for including in form for showing date of 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…

  11. zenith says:

    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

  12. zenith says:

    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

  13. owolabi tosin says:

    thanks for this post its very awesome

  14. AMMY says:

    thanks you

  15. sha says:

    Parse error: syntax error, unexpected ‘$getquery’ (T_VARIABLE) in C:\wamp\www\pract\comment.php on line 46

  16. ak says:

    how to display individual comment of a different blogs

  17. eglal says:

    thank you very much, it worked for me

  18. bruceewright says:

    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

  19. ayan khan says:

    sir, i need a admin pannel to delete or publish the comment.
    plz help..allah bless you.

  20. Matthew says:

    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))

  21. akshay says:

    Thank you

  22. Danny says:

    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

  23. kimuel metuda says:

    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..

  24. josiana says:

    the comments don’t get posted, there are no errors but the comments don’t get posted

  25. no_name says:

    how to do a comment like you

  26. no_name says:

    i mean like you page comment

  27. Harsh says:

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

  28. Rakesh says:

    thanks sir your coding is awesome

  29. Jason Robinson says:

    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 :)

  30. Jason Robinson says:

    update: problem fixed by putting the form and the code to handle it on different pages.

  31. james says:

    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

  32. james says:

    MD Mosleyh UDDIN

    thank u so much for the code finally it work hope more tiutorial come thank u ur the great php programmer:)

  33. hesho says:

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

  34. রাজু says:

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

  35. rahul says:

    Which name we give when we save the above file

  36. jagdeep says:

    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

  37. Stefan says:

    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

  38. dab130 says:

    how do you edit this code to where it will allow you to delete the old comments?

  39. Lucky says:

    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

    • binod says:

      that is your setting error. USE THIS>>>>include(‘config.php’);
      $insert=mysql_query(“INSERT INTO comments (name,comment) VALUES (‘$name’,’$comment’)”);
      echo ‘please fill all fields’;



  40. brushy says:

    how do i get more webpage in my search engine

  41. Austin says:

    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?

  42. aaaaaaaa says:

    how to delete comment data

  43. r10gordon90 says:

    am getting

    Parse error: syntax error, unexpected ‘$dbLink’ (T_VARIABLE) in C:\xampp\htdocs\comment\commentindex.php on line 8

    • faoud says:

      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

  44. ghanshyam says:

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

  45. diwakar says:

    showing : Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

    sow to resolve it.

  46. nokot says:

    it showing me error undefined index

  47. Nasser says:

    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

  48. trevor says:

    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! :)

  49. Bitcoin FAQ says:

    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:


    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.

  50. Khooshbu says:

    Thank You! Your Tutorial is awesome :-) i learned very well Thank you again :-)

  51. sandip says:


  52. Thangaraj says:

    Thank You! Your Tutorial in search Engine

    • angel says:

      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.

  53. Bhushan says:


  54. charles says:

    great work displayed

  55. selamu shiratwi says:

    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

  56. khan says:

    goooood website

  57. emily says:

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

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

  58. bhairab says:

    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

  59. bhairab says:

    thsi type of error flags what i will do..a alredy cross chk the code but i cant find the bugg

  60. Chris says:

    Very good. It’s working fine. Thank you!

  61. ronnie says:

    those three types of codes should be in one page? what would be the name of the php code.??

  62. පමෝද says:

    Thank you very much sir. This solved all my problems.. keep up the good work.

  63. mickle says:

    Going to try this out…

  64. Martin says:

    How do i change the comment output color it seems my comments doe snot show on blue background

  65. kelvin says:

    I think this code have a bug, try yo reload the page and your comment will be double

  66. Mahmood says:

    Thank you so much. Please provide php ecommerce tutorials.

  67. grinta says:

    is there a way to put a comment count for each post in this code

  68. hammad aarora says:

    very nice tutorial.

  69. rohit says:

    awesome tutorial….

  70. JB says:

    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..

  71. Shishir says:

    Sir… I’m a beginer…. Where is the video??

  72. Kamal Jeet says:

    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 !

  73. Manjit says:

    sir, comment system is great, please add captcha, please sir

  74. Rafi says:

    how commenting system php flat file get me idea

  75. vasto says:

    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 .TIA

  76. zara says:

    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.

  77. Manish says:

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


    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

  78. geeksmedia1 says:

    Very nice Information.

  79. Ummul Shifa says:

    Pretty nice piece of information

  80. Go Samaan Go says:

    Very good and inforative if you please share demo as well source file

  81. rjuri0103 says:

    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

  82. Roberto says:

    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

  83. kipp says:

    Nice one working good

  84. alex says:

    i am having this when i run my php
    ‘ . ‘
    ‘ . $comment . ‘
    ‘ . ‘
    ‘ . ” ;} ?>

  85. asmaa says:

    it showing me error undefined index

  86. asmaa says:

    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’] : ”;

  87. chimara says:

    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

  88. ritu says:

    how to make comment section in php with ajax

  89. ritu says:

    mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\blog\showcomment.php on line 9

  90. john says:

    Thank you, fantastic one.

  91. mzcodexf says:

    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.

  92. Ajay says:

    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…

  93. Amash says:

    I have completed all step.but in my server , my sql database has not auto increment,so when I post add only one

  94. sg says:

    give some more ,
    i am a beginner

  95. Admin says:

    nice one bro!

  96. Admin says:

    how do u always put your pix in ur reply as d admin cos I didn’t c anywhere I could upload a pix

  97. pradheep says:

    nice tutorial men , good job

  98. michael says:

    can i add a delete comments section ?

  99. DDGupta says:

    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’@’’ (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 *

eight + nine =

This site uses Akismet to reduce spam. Learn how your comment data is processed.