How to Insert form data in MySQL Table using PHP
Inserting form data into a MySQL database table is a common task in web development. It can be achieved using PHP by establishing a connection to the database, creating an SQL INSERT statement, and executing the statement with the form data. In this article, you will learn the basic steps involved in inserting form data into a MySQL table using PHP.
Create HTML Form
Before going for php code, let’s make an HTML form with two fields: name and email.
<html>
<head>
<meta charset="UTF-8">
<title>Create Record</title>
<style>
#registration_form {
background: rgb(17, 2, 23);
padding: 30px;
color: rgb(90, 7, 122);
}
#registration_form:hover {
box-shadow: 1px 1px 2px black, 0 0 25px rgb(90, 7, 122), 0 0 5px darkblue;
}
.btn-purple {
border-width: 2px;
border-style: inset;
border-color: #5a077a;
color: white;
}
.btn-purple:hover {
color: white;
box-shadow: 1px 1px 2px black, 0 0 25px rgb(90, 7, 122), 0 0 5px #4f4cff;
}
</style>
</head>
<body>
<div align="center" class="container py-5 my-5 col-5">
<form id="registration_form" method="post">
<h1>Create Your Account!</h1>
<input type="text" name="uname" placeholder="Name" />
<input type="email" name="uemail" placeholder="E-mail" required="required" />
<button type="submit" name="submit_btn" class="btn btn-purple">Submit</button>
</form>
</div>
In the above html code, we mapped name field with uname, email with uemail and submit button with submit_btn .
Steps in PHP Code for Inserting Data into Database from Form.
Here are the Steps on How to Insert form data in MySQL Table using PHP
Step 1 : Connect to the Database
Use the mysqli_connect() function to establish a mysql server connection and store the same in conn variable.
$conn=mysqli_connect("localhost","username","password","databasename");
Step 2: Collect the form data
Use the $_POST or $_GET superglobal arrays to collect the form data submitted by the user.
if(isset($_POST['submit_btn']))
{
/* Collecting form data in to php variables starts here */
$username =$_POST['uname'];
$mail =$_POST['uemail'];
/* Collecting form data in to php variables Ends here */
}
Step 3: Check whether the user is already registered from mysql data
Check whether the user is already registered or not from mysql data using select query
/* Trying to Check the user is already registered or not ! Starts Here */
$query="select * from user_detail WHERE `u_email`='$mail'";
$query_run=mysqli_query($conn,$query);
/* Trying to Check the user is already registered or not ! Ends Here */
if(mysqli_num_rows($query_run)>0)
{
#this block code is executed if the user is already registerd
echo "User already exits! Please try with another email id";
}
else
{
#Write your code here...
}
Step 4: Prepare the SQL INSERT statement for to insert data into table sql.
Create an SQL INSERT statement that specifies the table name and the columns to be inserted.
#storing user data in the table starts here
$query="insert into `user_detail` (`u_name`,`u_email`) VALUES('$username','$mail')";
$query_run=mysqli_query($conn,$query);
if($query_run)
{
echo "User Registered";
}
else
{
echo "Something Error! Please Contact Site Owner!!";
}
#storing user data in the table Ends here
PHP Code for Inserting Data into Database from Form
<?php
$conn = mysqli_connect("localhost", "username", "password", "databasename");
#isset statement block Starts here
if (isset($_POST["submit_btn"])) {
/* Collecting form data in to php variables starts here */
$username = $_POST["name"];
$course = $_POST["course"];
$mail = $_POST["email"];
$password = $_POST["pass"];
/* Collecting form data in to php variables Ends here */
$encrypted_password = md5($password); # password is converting in to md5 password
/* Trying to Check the user is already registered or not ! Starts Here */
$query = "select * from user_detail WHERE `u_email`='$mail'";
$query_run = mysqli_query($conn, $query);
/* Trying to Check the user is already registered or not ! Ends Here */
if (mysqli_num_rows($query_run) > 0) {
#this block code is executed if the user is already registerd
echo 'User already exits! Please try with another email id';
} else {
#storing user data in the table starts here
$query = "insert into `user_detail` (`u_name`,`u_email`,`u_pass`,`u_course`) VALUES('$username','$mail','$encrypted_password','$course')";
$query_run = mysqli_query($conn, $query);
if ($query_run) {
echo 'User Registered!';
} else {
echo 'Something Error! Please Contact Site Owner!!';
}
#storing user data in the table Ends here
}
}
#isset statement block ends here
?>
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 suggestions, please feel free to leave a comment below.