Scroll page back to the top

Simple commenting system with PHP and jQuery

Simple commenting system with PHP and jQuery

In today's series we'll be learning how to create a simple commenting system to allow visitors to leave their feedback on any section of the website.

Time: 37:43 min

Sebastian Sulinski on 6th Jan 2012

Download Exercise Files
 
 
Watch now!

Form structure (04:05 min)

 
Watch now!

Feedback database (01:52 min)

 
Watch now!

Fetch records (03:16 min)

 
Watch now!

Display comments (03:21 min)

 
Watch now!

JavaScript formObject (05:12 min)

 
Watch now!

Remove comment method (02:52 min)

 
Watch now!

Submit form method (03:19 min)

 
Watch now!

Add comment (05:18 min)

 
Watch now!

Return comment (04:40 min)

 
Watch now!

Delete comment (05:08 min)

 
 
 
 

Discussion (42 comments)

  • Tim

    Tim on Friday, 6th January 2012

    Hi Sebastian
    Is this comment system setup in such a way that visitor A can't delete visitor B's comment?
    Cheers

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Friday, 6th January 2012

    Hi Tim,
    As it stands - yes - this tutorial is to only show how to build the main structure of the commenting system. The restrictions etc. is a separate topic, although it shouldn't be very difficult - you simply have to figure out whether the logged in user id is the same as the 'author' id (once you have a login structure in place) and then allow removal.

    Reply

  • Tim

    Tim on Friday, 6th January 2012

    ...or I guess allowing the visitor the ability to remove a comment can be removed which means the admin would have to delete the comment from the back-end... 'an okay workaround'... do you think a captcha system would need to be integrated into something like this and if so how difficult would it be to implement you think?

    Reply

  • Tim

    Tim on Friday, 6th January 2012

    Also, maybe something that checks and confirms the user's IP is the same they logged the comment with before showing the remove link could be used.. not sure how this would be implemented.

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Friday, 6th January 2012

    That's one of the options - then the only way to remove the comment would be by the administrator. I personally wouldn't use the captcha on the commenting system - esthetically it doesn't look very nice. If you start getting large amount of spam comments then it's something to think about.

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Friday, 6th January 2012

    It wouldn't be the best option to track it by IP - as you know most of the people are using dynamic IPs, therefore one day you could remove a comment - the other someone else could remove yours.

    Reply

  • Lars Inge Holen

    Lars Inge Holen on Saturday, 7th January 2012

    Nice tutorial Sebastian as always :-)
    What it you first build it as a normal comment system and then maybe add how to delete comments?
    Most of us don't want just anybody to be able to delete comments.

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Saturday, 7th January 2012

    Hi Lars,
    Absolutely, but this tutorial is to just illustrate how to do commenting without any restrictions. If you have content management system ready, you can plug it in and do restrictions etc.

    The restriction would be something as simple as (having session user_id storing your logged in user id):

    if (!empty($_SESSION['user_id']) && $_SESSION['user_id'] == $row['user']) {
    	// display remove link here
    }
    

    You would obviously have table with users already, so the only thing you have to do is to add another table cell to the comments table called user, create index on it and set the relationship between id in the users table and user field in the comments table.

    You can still allow non-registered users to post comments, but since the above code will not recognise the session or user - it will not allow them to remove their comments.

    I hope this helps.

    Reply

  • Lars Inge Holen

    Lars Inge Holen on Saturday, 7th January 2012

    Sebastian,
    Ok, I think I misunderstood the purpose of this tutorial....
    Keep up the good work and have a nice day :-)

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Saturday, 7th January 2012

    Thanks Lars - you too.

    Reply

  • Lars Inge Holen

    Lars Inge Holen on Wednesday, 11th January 2012

    Sebastian,
    One more thing - hope you don't mind...
    In Norway we have 3 letters more in the alphabet รข? compared to the English alphabet. What can I change in the code so that they will be displayed in a correct way?

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Wednesday, 11th January 2012

    Hi Lars,
    To display these three characters correctly try to use the htmlentities() function with additional 2 parameters like so:

    htmlentities(stripslashes($row['full_name']), ENT_QUOTES, 'UTF-8');
    htmlentities(stripslashes($row['comment']), ENT_QUOTES, 'UTF-8');
    

    The second parameter (constant) simply tells the function how to handle quotes and the third one what character set to use in conversion. Both parameters are optional, but will help to solve the problem you are facing.

    You will have to add them in both index.php and comment.php.

    Here's more about this specific function : htmlentities.

    Reply

  • Lars Inge Holen

    Lars Inge Holen on Wednesday, 11th January 2012

    THANK YOU !!!
    GREAT !!
    You rock.

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Thursday, 12th January 2012

    Thanks Lars - you rock too! :)

    Reply

  • poor

    poor on Sunday, 15th January 2012

    Hello
    I think there is a Database Error
    Please help.

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Sunday, 15th January 2012

    Can you tell me a little bit more about your error - what does it say and when does it occur?

    Reply

  • ammar

    ammar on Monday, 23rd January 2012

    wow man your lessons are amazing .... Really amazing Thank you Sebastian .

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Monday, 23rd January 2012

    Thanks Ammar - I'm glad you like it.

    Reply

  • Andy

    Andy on Friday, 24th February 2012

    Thanks Sebastian, I just discovered your website and it went right on the favorite bar!
    I'm a designer/frontend and after a lot of years I decided to learn a little PHP building something. I just watched the first lesson, to understand how is the finished comment system and was exactly what I was trying to do!

    Any advice on making it threaded (reply to a comment?).

    Thanks
    Andy

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Friday, 24th February 2012

    Hi Andy,
    Thanks for the comment - I'm planning on doing one tutorial where I'm explaining how to build the Forum application, which will do exactly what you're looking for, but it might not be for another few months due to the work load I currently have.

    Reply

  • Andy

    Andy on Friday, 24th February 2012

    Thanks Sebastian, in the meanwhile I'll try to figure it out by myself using this tutorial as a starting point!

    Reply

  • radzeen

    radzeen : @gagodian on Wednesday, 14th March 2012

    Sebastian,
    I suggest to provide a demo page.
    It would really help me to understand what are you demonstrating in those tutorial before i dig further by downloading the exercise files or watching the tutorial.

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Wednesday, 14th March 2012

    Hi Radzeen,
    You can see what is going to be built in the first video - since I don't really have the time to moderate comments - I've decided against the demo for this project.

    Reply

  • Juan

    Juan on Monday, 26th March 2012

    Hi!,
    I've changed the database code in the index, but nothing.
    I can't do it, what I get is:

    "Fatal error: Call to a member function fetchAll() on a non-object in /home/a7668821/public_html/index.php on line 13"

    Thanks
    Nice website

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Monday, 26th March 2012

    Hi Juan,
    The error message simply tells you that you're referring to the fetchAll() method without using the object (class instance) as the reference. Did you instantiate the class $objDb in index.php and then use it to actually call the fetchAll() method like so? :

    $objDb->fetchAll();
    

    Reply

  • Juan

    Juan on Monday, 26th March 2012

    Thanks Seba, and sorry but why does it looks like this? My comment system on my web.
    http://centrodeamor.host22.com/

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Monday, 26th March 2012

    Hi Juan,
    It would indicate that the database username and / or password you've provided in the connection are incorrect. You need to check your database username and password and replace it in the PDO instantiation.

    Reply

  • Juan

    Juan on Monday, 26th March 2012

    Thanks sebas
    I will check it again and hope to get lucky.
    Very good site, good service ...

    Greetings!
    in God

    juan.

    Reply

  • Juan

    Juan on Friday, 30th March 2012

    Hi Seba :p sorry I'm back.
    I checked it all piece by piece but pulled the following bug:

    Fatal error: Call to a member function fetchAll() 
    on a non-object in /home/a5466829/public_html/index.php on line 13
    

    Which is the same as before.
    In that line is the following:

    $posts = $statement->fetchAll(PDO::FETCH_ASSOC);
    

    So what do you think?

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Friday, 30th March 2012

    Hi Juan,
    What do you have before the line 13 - that reads:

    $posts = $statement->fetchAll(PDO::FETCH_ASSOC);
    

    Can you paste the whole code before that here?

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Saturday, 31st March 2012

    Hi Juan,
    What it most probably is, is the returned value from the query() call. If the query() returns false then it is definitely not an object. The quick fix for this would be to do check if it's not false:

    $statement = $objDb->query($sql);
    if ($statement !== false) {
    	$posts = $statement->fetchAll(PDO::FETCH_ASSOC);
    } else {
    	$posts = null;
    }
    

    Reply

  • Dekip

    Dekip on Sunday, 1st April 2012

    HI, Sebastian.

    I'm trying to implement your code in one of my projects. The problem is that the row downloaded files doesn't work as yours in video. It's ok to change that "/" for included files but when i submit my comment it doesn't insert (load) that comment directly on page, instead i must reload to see it. Remove link do it's job, but submit nope.

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Monday, 2nd April 2012

    Hi Dekip,
    Check what's the ajax response using Firebug's Console tab - this should give you an idea of what might be causing the problem.

    Reply

  • Dekip

    Dekip on Monday, 2nd April 2012

    Well, i am not good in javascript or ajax, so that's why i ask for help. :(

    I put your script as i downloaded it from here on www.izostil.byethost14.com. I only change db connection parameters. FB console says something about deprecated things if i get it right. Attributes nodeName and nodeValue that have to be changed with name and value. But, i didn't find such attributes.

    Any clue would be helpfull...

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Tuesday, 3rd April 2012

    Hi Dekip,
    You get the 302 error, which is explained here a bit more : 302 Error.

    I can see you've put a few lines of code there as well so I can't really examine the response properly. Also - why is it calling /mod/index.php after calling /mod/comment.php ?

    Reply

  • Dekip

    Dekip on Tuesday, 3rd April 2012

    Hi, Sebastian.

    The whole time i messed up with my xampp instalation becouse your code won't work on it. I even uploaded the wrong code on server. Now it's the right one and it works. On server, not on my xampp.

    Thank you for your code and your time, Dejan.

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Tuesday, 3rd April 2012

    No problem Dekip - glad you've solved the problem.

    Reply

  • Izify

    Izify : @izify1twitter.com on Thursday, 3rd May 2012

    Keep up the good work and have a nice day :-)

    Reply

  • James Avakian

    James Avakian : @Facebook page on Tuesday, 8th May 2012

    I am having a problem with PDO, when I open MAMP and Dreamweaver with this index.php, I set the .sql file inside phpmyadmin, the php cannot accept any of the 'TRY' statement in the top of the php file? why?

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Friday, 11th May 2012

    Hi James,
    I'm not sure, but it might be Dreamweaver's behaviour - what errors do you get?

    Reply

  • michael gomez

    michael gomez on Saturday, 19th May 2012

    Hi

    It got it working, however, I have 3 things wanted to know about:
    1) how i make the comments to appear to the relevant post not to all webpage?
    2) is there a way I can use the comment system without entering the '$objDb = new PDO('mysql:host=localhost;dbname=feedback', 'root', '9u5jVjqKDX0O2t');' above the php file, I dont want anybody looking at my password
    3) can you suggest and script to verify email input?

    thank you

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Monday, 21st May 2012

    Hi Michael,
    To make comments appear with the relevant post you would have to add another field to the comments table called for instance 'post', which would store the id of the post record. You could then populate only the comments that have the relevant post id and display them with the post.

    The PDO connection parameters aren't visible to anyone so you shouldn't be worry about it. If for any reason you see the connection string that would suggest that you're not running PHP on your server or the file you put the connection string within is not a PHP file. Alternatively if you simply want to have it in one place without putting it into each, separate file whenever you have to interact with the database, you could create a PDO class wrapper and only instantiate the Database class (the wrapper) and call its methods to process your requests.

    For email validation there are many scripts, but the easiest way is to use PHP Validation Filter : FILTER_VALIDATE_EMAIL, which isn't super fantastic, but does the job.

    I hope this helps.

    Reply

 
 
Add a comment
Add Comment