This tutorial we will give you details instruction on creating the PHP contact form script with MySQL to save the content of the form to the database. As you know, an admin needs to visit their email address to read the customer’s email content. By using this solution, the website administrator can check all customer’s email content on the website back office. It is absolutely helpful to manage your customer as well as order information. product color, size, etc. in the back end.

PREPARATION CODE

We will use the PHP send email script that we have prepared. In the tutorial, we will only write more PHP and MYSQL code. You can download code of php code to send email : download code here

The code is simple PHP & MySql code. You can’t use the code if you are using a framework like Prestashop or Magento.

We use PhpMyAdmin to create database & table. You have a look PhpMyAdmin here

Alright, let start.

Create a new database called “project4”

  • Mysql Database: You need to create a new database. In this new database, you should create a database table which includes 5 fields: ID, name, phone, email, message text, and send-date.

PHP contact form script with MySQL

Use these codes to create DB:


CREATE TABLE contact_form (

id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,

name VARCHAR(30) NOT NULL,

phone varchar(13),

email VARCHAR(50),

message text,

send_date TIMESTAMP
)

Screenshot for this step:

php contact form script with mysql

HTML file:Add phone field


<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Your Phone *</label>
<input type="text" value=""  maxlength="100" class="form-control" name="phone" id="phone" required="" aria-required="true">
</div>
</div>
</div>

Please refer to screenshot below:

PHP contact form script with mysql

PHP file

We will connect database by putting the below codes into the first row of PHP file.


// create connect
$conn = new mysqli('localhost', 'root', '123456', 'project4');
// check connect
if ($conn->connect_error) {
die("connect fail: " . $conn->connect_error);
}

The meaning of each value:

  • localhost: the server address
  • root: user to connect database
  • 123456: password to connect database
  • project4: database name

Note:

  • If there has a warning message why trying to connect, you need to check your server parameters as well as your codes.
  • You can create these values in the cPanel or ask your hosting/service provider

Save the form content into the database.


//update
//  SQL Insert
$sql = "INSERT INTO contact_form ( name, phone,email,message)
VALUES ('$name','$phone', '$email','$message')";
// add record
if ($conn->query($sql) === TRUE) {
echo "add record complete";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

PHP contact form script with mysql

That’s all. Now we can test if the form is working or not by below steps:

  • Step 1: Submit a form in the contact form of the storefront
  • Step 2: Check if those form contents are saved in the database or not.

This is my test result when I follow step by step of the PHP contact form script with mysql tutorial.

PHP contact form script with mysql

If there are any errors, please check all steps/codes again by watching our video guidance.

Below is the full source code: Please download code here :

If you have any questions, please contac us. Please share this tutorial so more and more people can have a chance to create their own php contact form script with mysql. If you are using shopify, you do not have to undestand every line of code because it has an available app Powerful form builder  that supports for free to create form simply, add many fields and data types.

 

 

 

Leave a Reply

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