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 or save or store your form data into a MySQL database table using a simple PHP script.

PHP Insert Data Into MySQL

Just follow the few below steps and easily insert 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 update the below code into your 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="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
    <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</label>
                            <input type="text" name="name" class="form-control">
                        </div>

                        <div class="form-group">
                            <label>Email</label>
                            <input type="email" name="email" class="form-control">
                        </div>

                        <div class="form-group">
                            <label>Mobile</label>
                            <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);
}
?>

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.

Leave a Reply

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