Create PHP comment box using MySQL database and Store entered comments into DB

How to make awesome stylish comment input box in PHP language for my website.

In this tutorial we are creating comment input box for websites into PHP scripting language. We are storing comments directly into MySQL database so website admin can assess them using PhpMyAdmin control panel. This forms are designed with custom style CSS code that makes the input files more big and gives hover effects. So here is the complete step by step tutorial for Create PHP comment box using MySQL database and Store entered comments into DB.

android-project-download-code-button

How to Create PHP comment box using MySQL database and Store entered comments into DB.

Code for comment.php file.

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Comment Box</title>
<style>

body{
 margin:0px;
 font-family:Baskerville, 'Palatino Linotype', Palatino, 'Century Schoolbook L', 'Times New Roman', serif;
 }

input[type=text], select {
 width: 100%;
 border-radius: 5px;
 margin: 7px 0;
 border: 1px solid #ccc;
 padding: 14px 18px; 
 display: inline-block;
 box-sizing: border-box;
}

input[type=submit]:hover {
 background-color: #00a7d1;
}

textarea, select {
 width: 100%;
 border-radius: 5px;
 margin: 7px 0;
 border: 1px solid #ccc;
 padding: 14px 18px; 
 display: inline-block;
 box-sizing: border-box;
}

input[type=submit] {
 width: 100%;
 border: none;
 color: white;
 padding: 14px 20px;
 background-color: #01c9fb;
 margin: 8px 0;
 cursor: pointer;
 border-radius: 4px;
 
}

</style>

</head>
<body>
<table bgcolor="#f2f2f2" style="padding:50px" align="center">
<form action="" method="post">
<tr>
<td> Name : </td><td><input type="text" name="name"></td>
</tr>
<tr>
<td> Email : </td><td><input type="text" name="email"></td>
</tr>
<tr>
<td> Website : </td><td><input type="text" name="website"></td>
</tr>
<tr>
<td> Comment : </td><td><textarea name="comment" rows="6" cols="50"></textarea></td>
</tr>
<tr>
<td><input type="submit" name="submit"></td></tr>

</form>
</table>



<?php
if(isset($_POST["submit"]))
{
 
 //Including dbconfig file.
include 'dbconfig.php';
 
$name = $_POST["name"];
$email = $_POST["email"];
$website = $_POST["website"];
$comment = $_POST["comment"];


mysql_query("INSERT INTO comment_table (name,email,website,comment) VALUES ('$name','$email','$website','$comment')"); 

echo '<center> Comment Successfully Submitted </center>';

}

 ?>

</body>
</html>

Code for dbconfig.php file.

 <?php

 //This script is designed by Android-Examples.com
//Define your host here.
$hostname = "localhost";

//Define your database username here.
$username = "root";

//Define your database password here.
$password = "";

//Define your database name here.
$dbname = "test";

 $conn = mysql_connect($hostname, $username, $password);
 
 if (!$conn)
 
 {
 
 die('Could not connect: ' . mysql_error());
 
 }
 
 mysql_select_db($dbname, $conn);

 //This script is designed by Android-Examples.com

?>

Screenshot:

Create PHP comment box using MySQL database and Store entered comments into DB

Click here to download Create PHP comment box using MySQL database and Store entered comments into DB project with source code + MySQL database included.

20 Comments

  1. This project is running fine.
    I have gone through other project also…they are also good…
    Nice job…..

  2. i like it too ………….

  3. Thank you for the code. its really works. nice

  4. great post, very informativge just the answer i was looking for

  5. Hello
    Is it mandatory to use PhpMyadmin for this comment box. Because I am using Netbeans IDE and I want to use either embedded Java database or an connecting with an external oracle SQL developer. Will it still work?

  6. Hello
    My question is where is the code for displaying the comments on the page? You need to fetch them from the database and show them. Where have you done that?

  7. mysql_connect is deprecated so I will need to use mysqli_connent

Leave a Reply

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