9 Steps to Create Simple PHP Comment System in PHP & MySql

This is Simple PHP Comment System in PHP & MySQL. We are going to create the PHP comment system with these functionalities. First of all, we are going to create comments database table. Then we will work inserting comments through comment form. Then we will create a few admin pages to moderate comments such as publish, edit & update, delete existing comments. This will be built from the scratch with simple PHP & MySQL.

1. Create a Database Table

Create a database with any name, but create a database table named comments with the following columns.

  • ID is integer type & autoincreement
  • Name is varchar type, it is used to store name field from comment form.
  • E-Mail is varchar type, it is used to store the email from comment form.
  • Subject is varchar type, it is used to store the comment text from comment form.
  • Submittime is datetime type, it is used to store the time of comment submission by setting default to current_timestamp.
  • Status is also varchar, it is used to store the status of comment. That is comment is published or not.

2. Connecting to Database & Selecting Database

In this second step, we are going to connect to the database & also selecting a database. If you want to learn more about connecting to the database & selecting database, go through below articles.

Save this below code as connect.php

3. Creating Comment HTML Form

In this step, I’ll create Comment HTML form with bootstrap styles. For that first, I’m loading bootstrap CDN files in the head section.

After adding above bootstrap CDN files, I’m creating a comment form with these fields: name, email, comment. You can copy paste this below code, I’ve applied all the bootstrap classes for good looking comment form.

4. Inserting Submitted Form Data into Database Table

Before inserting the submitted form data we need to connect to the database. In the previous step we have already created the connect.php file. Just load connect.php in current PHP file with require PHP function. save this file as commentform.php

Afer connecting to the database, we have to insert submitted data into the database. For that first of all, I’m checking if post super global is set and not empty. Then I’m using mysqli_real_escape_string to escape special characters. I’m applying this function to all the fields name, email & comment. And then assigning these values to variables.

Next is creating INSERT SQL query to insert the submitted form data. Then executing SQL query with mysqli_query function. If it’s successful displaying success message or else displaying failure message.

These success and failure messages should be displayed above comment form. Add this below code inside panel body div of our comment form code.

5. Displaying Comments in Back-end

Before this step, you should build admin login system. Because these operations can only be done by an admin or moderator user. If you don’t know how to create a login system, follow this tutorial.

Simple Login Script in PHP and MySql

Add this below HTML code to a file and save it as viewcomments.php inside admin directory. Don’t forget to add bootstrap CDN files.

After adding the above table, then fetch the comments from comments database table using select SQL query. Then execute the SQL query using mysqli_query.

After that fetch the result rows in an associative array using mysqli_fetch_assoc PHP function, and by using while loop I’m looping through associative array then displaying all these comments in the above HTML table.

6. Edit & Update Comments

In the last step, I’ve already created the anchor link to edit text under operations and also passing the id in the URL for editcomment.php

Create a php file with the name of editcomment.php and add the below code to it with basic html and bootstrap CDN files.

We will load the file if get super global is set and not empty or else we will redirect the user to main comments page with PHP header function.

Then I’m assigning the id value passed through URL to id variable. And selecting the comment with that specific id, if the comment exists then we will display the comment data in above form or else we will redirect the user to the main comments page.

Displaying name data from comments table in name input field.

Displaying Email data from comments table in Email input field.

Displaying Comment data from comments table in comments text area field.

Selecting the correct comment status for the current comment.

After making proper changes to update comment, then moderater submitts the form. Then we will check if the post super global is set and not empty. If it’s set we will check for any special characters with mysqli_real_escape_function PHP function.

With update sql query we will update the comment, then assign the success message or failure message.

These success and failure messages should be displayed above edit comment form. Add this below code inside panel body div of our edit comment form code.

7. Comment Status Update

Next, add the links in viewcomments.php page insider operations section. Previously we have already created the anchor links with # links, just update them with below code. In this code, I’m sending comment ID and status of the comment to be updated.

Add this below code to commentstatus.php file, before that load connect.php file with require PHP function. In this code, I’m checking if the get super global is set and not empty. Assigning id and status to variables. Then with update sql query, I’m updating status of the comment with id.

After updating comment redirecting the user to comments.php file with PHP header function.

8. Deleting Comments

Update the delete anchor link in viewcomments.php file with this below code. Here I’m passing only id through url.

add this code to php file and save it as delcomment.php. By using the id passed from url, I’m writing delete query to delete the comment. Then redirect user to veiwcomments.php file

9. Displaying Comments with Profile Pic

Save this code as displaycomments.php file. Here I’m displaying the list of all comments with gravatar image. But if you relate it to content then you can specify the comments with that content in where clause.

If you want to know more about using gravatar image go through this article => How to Get Gravatar Image Using PHP

Till now you have learned to create PHP comment system, now you can work on creating reply for this comments from moderators or admins.

Complete code of PHP Comment System in PHP & MySQL

If you have any problem arranging above pieces of code. You can use this complete code.

If you have any issues, let me know through below comment form.

add-to-cart php?ID= add to cart code in php simple shopping cart php simple registration and log in form php and mysql prigram in php to implement registration php full registration list of softwares to develop php list of free web ide free ide for laravel used india software to make website in php
Vivek Vengala

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: