PHP MySQL Contact us Form with database

https://laratutorials.com/php-mysql-contact-us-form-with-database/

PHP MySQL Contact us Form with database

Hello php programmers and developers, PHP MySQL contact us form with validation. In this post, i will teach you how to create simple php contact us form & storing in mysql database.

As well as you can use this code personally and professionally.

Description :- i will create database for store/save contact us form data. Then will create database connecting file and contact us form in Html. After that, will create store contact us form data and thank html page.

How to Create a Contact Form with PHP

Follow the below given simple steps to create simple contact us form in PHP MySQL and store it into database:

Step 1 – Requirement and Create Directory

In step 1, Navigate to your local web server directory. And inside this directory, create one directory. And you can name this folder anything.

Here, I will “demo” the name of this folder. Then open this folder in any text editor (i will use sublime text editor).

Step 2 – Create phpmyadmin MySQL Database and Connecting File

In step 2, you need to create database and table. So run the following sql query to create database and table:CREATE DATABASE my_db;CREATE TABLE `contact_us` ( `id` int(10) UNSIGNED NOT NULL, `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `message` text COLLATE utf8mb4_unicode_ci NOT NULL, `created_date` date DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Then create a php file that named mydbCon. Which is used to connect phpmyadmin mysql database to project (demo).

So, now create mydbCon file and add the below given code into your file:<?php $hName='localhost'; // host name $uName='root'; // database user name $password=''; // database password $dbName = "my_db"; // database name $dbCon = mysqli_connect($hName,$uName,$password,"$dbName"); if(!$dbCon){ die('Could not Connect MySql Server:' .mysql_error()); }?>

Step 3 – Create Contact Us Form In HTML

In step 3, create a php file that named contact-us.php. This file will display PHP MySQL contact us form and store form data into MySQL database with validation.

Here, i will use html validation with PHP MySQL contact us form. Now, you need to add the following contact us form code into contact-us.php file:<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>PHP Contact US Form with Database and Validation</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"></head><body><div class="container mt-2"> <div class="row"> <div class="col-md-12"> <div class="page-header"> <h2>PHP MySQL Contact US Form with Database</h2> </div> <form action="store-contact-us.php" method="post"> <div class="form-group"> <label>Name</label> <input type="text" name="name" class="form-control" required=""> </div> <div class="form-group"> <label>Email</label> <input type="email" name="email" class="form-control" required=""> </div> <div class="form-group"> <label>Message</label> <input type="text" name="massage" class="form-control" required=""> </div> <input type="submit" class="btn btn-primary" name="submit" value="submit"> </form> </div> </div></div></body></html>

Step 4 – Store Contact Us Form to Database

In step 4, create new file that named store-contact-us.php file. Because this is used to store contact us form data into mysql database.

So, add the below code into your store-contact-us.php file:<?phpif(count($_POST)>0){ include 'mydbCon.php'; $name = $_POST['name']; $message = $_POST['message']; $email = $_POST['email']; $query = "INSERT INTO contact_us (name,message,email) VALUES ('$name','$message','$email')"; mysqli_query($dbCon, $query); $lastId = mysqli_insert_id($dbCon); if (!empty($lastId)) { $message = "Your contact information is saved successfully"; }} header ("Location: thank-you.php");?>

Step 5 – Create Thank You Html Page

In step 5, create thank-you.php file. After successfully saved contact us form data in PHP MySQL database. Then i will redirect to thank you page.

Add the following code into thank-you.php file:<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Thanks for Contact US - PHP MySQL</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"></head><body><div class="container mt-2"> <div class="row"> <div class="col-md-12"> <div class="page-header"> <h2>Thanks for contacting us. We will get back you.</h2> </div> </div> </div></div></body></html>

Last updated