PHP Code: Insert Data Into MySQL Database From Form

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());
		}
?&gt;

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

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);
}
?&gt;

Conclusion

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.

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

One reply to PHP Code: Insert Data Into MySQL Database From Form

  1. Good job Devendra.

Leave a Reply

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