How to Insert Form Data into Database using PHP ?
Last Updated :
21 Apr, 2025
In modern web development, Handling User Input is a fundamental task. One of the most common Operations is capturing data from a form and inserting it into database. PHP, combined with MySQL, offers a straightforward and powerful way to achieve it.
Here, we will see complete process of inserting form data into MySQL database using PHP - from setting up the database to writing secure and efficient code.
Prerequisites:
HTML form: First we create an HTML form that need to take user input from keyboard. HTML form is a document which stores information of a user on a web server using interactive controls. An HTML form contains different kind of information such as username, password, contact number, email id etc.
The elements that are used in an HTML form are check box, input box, radio buttons, submit buttons etc. With the help of these elements, the information of an user is submitted on the web server. The form tag is used to create an HTML form.
Syntax:
<form> Form Elements... </form>
or
To pass the values to next page, we use the page name with the following syntax. We can use either GET or POST method to sent data to server.
<form action=other_page.php method= POST/GET>
Form Elements...
</form>
Note: XAMPP stands for cross-platform, Apache, MySQL, PHP, and Perl. It is among the simple light-weight local servers for website development. In PHP, we can connect to database using localhost XAMPP web server.
Step 1: Start the XAMPP Server
- Open the XAMPP Control Panel.
- Start Apache and MySQL.

Step 2: Open localhost/phpmyadmin in your web browser and create database with database name as staff and click on create.

Step 3: Then create table name college.

Step 4: Enter columns and click on save

Syntax for Database Connection
PHP
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
// Create connection
$conn = new mysqli($servername,
$username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: "
. $conn->connect_error);
}
$sql = "INSERT INTO `table` VALUES
('John', 'Doe', '[email protected]');";
if ($conn->query($sql) === TRUE) {
echo "record inserted successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
Step 5: Now open Notepad and start writing PHP code and save it as index.php and open other notepad and save it as insert.php Save both files in one folder under htdocs.

Save Filename as: index.php
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>GFG- Store Data</title>
</head>
<body>
<center>
<h1>Storing Form data in Database</h1>
<form action="insert.php" method="post">
<p>
<label for="firstName">First Name:</label>
<input type="text" name="first_name" id="firstName">
</p>
<p>
<label for="lastName">Last Name:</label>
<input type="text" name="last_name" id="lastName">
</p>
<p>
<label for="Gender">Gender:</label>
<input type="text" name="gender" id="Gender">
</p>
<p>
<label for="Address">Address:</label>
<input type="text" name="address" id="Address">
</p>
<p>
<label for="emailAddress">Email Address:</label>
<input type="text" name="email" id="emailAddress">
</p>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Save Filename as: insert.php
HTML
<!DOCTYPE html>
<html>
<head>
<title>Insert Page page</title>
</head>
<body>
<center>
<?php
// servername => localhost
// username => root
// password => empty
// database name => staff
$conn = mysqli_connect("localhost", "root", "", "staff");
// Check connection
if($conn === false){
die("ERROR: Could not connect. "
. mysqli_connect_error());
}
// Taking all 5 values from the form data(input)
$first_name = $_REQUEST['first_name'];
$last_name = $_REQUEST['last_name'];
$gender = $_REQUEST['gender'];
$address = $_REQUEST['address'];
$email = $_REQUEST['email'];
// Performing insert query execution
// here our table name is college
$sql = "INSERT INTO college VALUES ('$first_name',
'$last_name','$gender','$address','$email')";
if(mysqli_query($conn, $sql)){
echo "<h3>data stored in a database successfully."
. " Please browse your localhost php my admin"
. " to view the updated data</h3>";
echo nl2br("\n$first_name\n $last_name\n "
. "$gender\n $address\n $email");
} else{
echo "ERROR: Hush! Sorry $sql. "
. mysqli_error($conn);
}
// Close connection
mysqli_close($conn);
?>
</center>
</body>
</html>
Note: Type localhost/7058/index.php in your browser, it will display the form. After submitting the form, the form data is submitted into database.

Enter Data into the form like First Name , Last Name , Gender, Email Address, Address .

Data stored in Database Successfully

Validate Your Form Submission in phpMyAdmin

Storing User Input into a MySQL database using PHP is a foundational step in developing dynamic and data-driven web applications.
Important Points You Need To Keep In Mind:
- Form validation- Add client side(JavaScript) and Server-side(PHP) validation to ensure data integrity.
- Use Prepared Statements: Prevent SQL Injection attacks by Prepared Statements with PDO or mySQLi.
- Display stored Data: Create a page to fetch and display records in a structured table.
- CRUD Functionality: Implement full CRUD operations- Create, Read , Update and Delete to manage Data Effectively.
- Email Notifications: Send confirmation emails to users after form submission using PHP
mail()
or SMTP. - User Interface Improvements: Style the form using CSS or frameworks like Bootstrap for a better user experience.
- Authentication System: Add a login and registration system to protect access to data and backend operations.
Similar Reads
How to fetch data from Database in PHP PDO using loop ?
The PDO (PHP Data Objects) defines the lightweight, consistent interface for accessing databases in PHP. Approach: Make sure you have XAMPP or WAMP installed on your windows machine. In case you're using Linux then install the LAMP server. In this article, we will be using the XAMPP server. Follow
4 min read
How to Upload Image into Database and Display it using PHP ?
Uploading the image/videos into the database and displaying it using PHP is the way of uploading the image into the database and fetching it from the database. Using the PHP code, the user uploads the image or videos they are safely getting entry into the database and the images should be saved into
8 min read
How to append data in JSON file through HTML form using PHP ?
The purpose of this article is to append data to a JSON file through HTML form using PHP. Approach 1: If the JSON file is not created then we create a new JSON file, send data to it, and append data in it. To see how to create a JSON file by taking data from the HTML form, refer this link. Approach
4 min read
Signup form using PHP and MySQL Database
The task is to create and design a sign-up form in which if the user enters details, the HTML form data are inserted into our MySQL server database. Approach: First task is that we have to create our MySQL server Database and a Table according to our requirements. We connect our MySQL server Databas
4 min read
How to Export data to CSV file from Database using XAMPP ?
In this article, we are going to load the data present in the database (MySQL) into CSV file. Display the data in the database and also export that data into CSV file. We are using XAMPP tool to store locally in a database. XAMPP stands for  cross-platform, Apache, MySQL, PHP, and Perl. It is among
3 min read
How to Display Data from CSV file using PHP ?
We have given the data in CSV file format and the task is to display the CSV file data into the web browser using PHP. To display the data from CSV file to web browser, we will use fgetcsv() function. Comma Separated Value (CSV) is a text file containing data contents. It is a comma-separated value
2 min read
How to fetch data from localserver database and display on HTML table using PHP ?
In this article, we will see how we can display the records in an HTML table by fetching them from the MySQL database using PHP. Approach: Make sure you have XAMPP or WAMP server installed on your machine. In this article, we will be using the WAMP server. WAMP Server is open-source software for th
4 min read
How to connect the Database with PHP DOM page ?
In this article, we will see how to connect the database with the PHP DOM page? Approach: Make sure you have XAMPP or WAMP installed on your machine. In this tutorial, we will be using the XAMPP server. To connect the database with the PHP DOM page, follow the steps given below: 1. Create Database:
3 min read
PHP program to fetch data from localhost server database using XAMPP
In this article, we will see how we can display the records by fetching them from the MySQL database using PHP. Approach: Make sure you have an XAMPP server or WAMP server installed on your machine. In this article, we will be using the XAMPP server.XAMPP is a free and open-source cross-platform web
4 min read
How to display XML data in web page using PHP ?
In this article, we are going to display data present in an XML file on a web page using PHP through the XAMPP server. PHP is a server-side scripting language that is mainly for processing web data. The XML stands for an extensible markup language. Requirements: XAMPP server Syntax: <root>
2 min read