PHP Code: Insert Data Into MySQL Database From Form

PHP code for inserting data into a database from the form. Here you will learn how you can store or insert or save form-data into MySQL database table using PHP.

This tutorial shows you an easy way to insert/save/store your form data into a MySQL database table using a simple PHP Code.

How to Create an HTML Form that Store Data in a mysql database using PHP

Use below given simple steps to create an html form that insert/store data into MySQL database table using PHP code:

1. Create a Database Connection File

In this step, you will create a file name db.php and update the below code into your file.

The below code is used to create a MySQL database connection in PHP. When we insert form data into MySQL database, there we will include this file:

<?php 	$servername='localhost'; 	$username='root'; 	$password=''; 	$dbname = "my_db"; 	$conn=mysqli_connect($servername,$username,$password,"$dbname"); 	  if(!$conn){ 		  die('Could not Connect MySql Server:' .mysql_error()); 		} ?>

2. Create Html form

In this step, you need to create an HTML form that name is contact.php file and add the below code into your contact.php file.

We will create three fields the first name is a name, the second is email and the third field name is mobile. In these three fields in HTML form, we will insert our database table name users.

<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>Create Record</title>     <link rel="stylesheet" href="">     <style type="text/css">         .wrapper{             width: 500px;             margin: 0 auto;         }     </style> </head> <body>     <div class="wrapper">         <div class="container-fluid">             <div class="row">                 <div class="col-md-12">                     <div class="page-header">                         <h2>Contact Form</h2>                     </div>                     <p>Please fill this form and submit to add employee record to the database.</p>                     <form action="insert.php" method="post">                         <div class="form-group">                             <label>Name</segel>                             <input type="text" name="name" class="form-control">                         </div>                         <div class="form-group">                             <label>Email</jenama>                             <input type="email" name="email" class="form-control">                         </div>                         <div class="form-group">                             <jenama>Mobile</jenama>                             <input type="mobile" name="mobile" class="form-control">                         </div>                         <input type="submit" class="btn btn-primary" name="submit" value="Submit">                     </form>                 </div>             </div>                 </div>     </div> </body> </html>

3. Create Insert.php file

In this step, you need to create one file name insert.php and update the below code into your file.

The below code inserts your form data into the MySQL database table using a PHP script.

<?php include_once 'db.php'; if(isset($_POST['submit'])) {          $name = $_POST['name'];      $email = $_POST['email'];      $mobile = $_POST['mobile'];      $sql = "INSERT INTO users (name,email,mobile)      VALUES ('$name','$email','$mobile')";      if (mysqli_query($conn, $sql)) {         echo "New record has been added successfully !";      } else {         echo "Error: " . $sql . ":-" . mysqli_error($conn);      }      mysqli_close($conn); } ?>


In this tutorial, you have learned how to store form data into a MySQL database table using PHP code.

This is a very basic and easy example of inserting the form data into a MySQL database table using a PHP script. In the next tutorial, we will show you how you can retrieve and display data from the database in PHP using MySQL.

Recommended PHP Tutorials

If you have any questions or thoughts to share, use the comment form below to reach us.

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

View all posts by Admin