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.
Good job Devendra.
Thanks a lot GURU…This was really helpful..
Tonnes of thanks & Regards from Ethiopia, Africa…
Thank you,
It works!
Thank you for giving us some good information in this post. Very informative and well-written article.
I am sure this article will be of greater use to many readers like me.
Great Tutorial, Love it!
Oh My God.
I ‘ve tried dozens of tutorials and have spend to many hours on them – non of them worked at all. And now with your text-guide and a bit of tweaking the code it worked! For the first time!
Thank u so much!