PHP Code Insert/Store Data Into MySQL Database From Form

connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}

// Get data from form
$name = $_POST[‘name’];
$email = $_POST[’email’];

// Insert data into MySQL
$sql = “INSERT INTO users (name, email)
VALUES (‘$name’, ‘$email’)”;

if ($conn->query($sql) === TRUE) {
echo “New record created successfully”;
} else {
echo “Error: ” . $sql . “
” . $conn->error;
}

$conn->close();

?>
[ad_1]

PHP code for inserting data into database from form using mysql. Through this tutorial, you will learn how to store or insert/save/store form data into MySQL database table using PHP.

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

How to Insert HTML Form Data in Database from From in PHP MySQL

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

  • 1. Create a Database Connection File
  • 2. Create Html form
  • 3. Create Insert.php file to Store Data Into Database using PHP Script

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="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 to Store Data Into Database using PHP Script

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.

Recommended PHP Tutorials

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

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00