ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.4 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.geeksforgeeks.org/php/how-to-insert-form-data-into-database-using-php/ |
| Last Crawled | 2026-03-26 18:19:24 (11 days ago) |
| First Indexed | 2025-05-30 00:00:01 (10 months ago) |
| HTTP Status Code | 200 |
| Meta Title | How to Insert Form Data into Database using PHP ? - GeeksforGeeks |
| Meta Description | Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more., Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions. |
| Meta Canonical | null |
| Boilerpipe Text | Last Updated :
23 Jul, 2025
Inserting form data into a MySQL database using PHP allows web applications to store user inputs like registrations or feedback. By collecting and validating the data from HTML forms, PHP securely inserts it into the database, ensuring data integrity and protection against security risks.
Here, we will see the complete process of inserting form data into a
MySQL
 database using
 PHP
, from setting up the database to writing secure and efficient code.
Requirements
Before you begin, ensure you have the following tools ready:
A Web Server with
PHP
Installed (e.g.,
XAMPP
,
WAMP
,
MAMP
)
A database system to store the form data(e.g.,
MySQL
or
MariaDB
)
A Working Code Editor (e.g.,
VS Code
,
Sublime Text
)
Quick Overview of HTML form
An
HTML form
is used to collect information from users on a website. It helps store data like usernames, passwords, contact numbers, and emails on a web server using interactive elements.
We use the
HTML <Form> Tag
to create forms on our website. Below is the syntax:
<form> Form Elements... </form>
Note:
To pass the values to the next page, we use the page name with the following syntax. We can use either the GET or POST method to send data to the server.
<form action=other_page.php method= POST/GET>
Form Elements...
</form>
Note:
In PHP, we can connect to the database using the localhost
XAMPP web server
. XAMPP stands for cross-platform,
Apache
, MySQL, PHP, and
Perl
. It is among the simple, lightweight
,
local servers for website development.
Insert Form Data into Database Using PHP (Step-by-Step)
Step 1:
Start the XAMPP Server
Open the
XAMPP Control Panel
.
Start
Apache
(for PHP) and
MySQL
(for database).
Step 2: Create the Database
Open
localhost/phpmyadmin
in your web browser.
Create a new database and name it
staff then c
lick
Create
.
Step 3: Create the TableÂ
In the
staff
database, create a new table named
college
.
Add columns to the table (e.g.,
first_name
,
last_name
,
gender
,
email
), then click
Save
.
Step 4: Establish a Database Connection in PHP
The database connection allows your PHP code to interact with MySQL, enabling data storage and retrieval.
It
refers to connecting your PHP code to a database so you can save and get data. This lets your website store things like user information and display it when needed.
This is the PHP code to establish a connection with a MySQL database.
<?php
$servername
=
"localhost"
;
$username
=
"root"
;
$password
=
""
;
$dbname
=
"staff"
;
// Create connection
$conn
=
new
mysqli
(
$servername
,
$username
,
$password
,
$dbname
);
// Check connection
if
(
$conn
->
connect_error
)
{
die
(
"Connection failed: "
.
$conn
->
connect_error
);
}
?>
Step 5: Create PHP Files
Open a text editor (e.g., Notepad) and create two PHP files.
index.php
(for the form)
insert.php
(to insert form data into the database)
Step 6: Write the PHP Code to Insert Data
Here’s the code for the form and the code to insert data into the database:
HTML Form (
index.php
):
PHP Code to Insert Data into the Database (
insert.php
):
<?php
$servername
=
"localhost"
;
$username
=
"root"
;
$password
=
""
;
$dbname
=
"staff"
;
// Create connection
$conn
=
new
mysqli
(
$servername
,
$username
,
$password
,
$dbname
);
// Check connection
if
(
$conn
->
connect_error
)
{
die
(
"Connection failed: "
.
$conn
->
connect_error
);
}
// Collect and sanitize form data
$first_name
=
mysqli_real_escape_string
(
$conn
,
$_POST
[
'first_name'
]);
$last_name
=
mysqli_real_escape_string
(
$conn
,
$_POST
[
'last_name'
]);
$gender
=
mysqli_real_escape_string
(
$conn
,
$_POST
[
'gender'
]);
$address
=
mysqli_real_escape_string
(
$conn
,
$_POST
[
'address'
]);
$email
=
mysqli_real_escape_string
(
$conn
,
$_POST
[
'email'
]);
// Insert data into database
$sql
=
"INSERT INTO college (first_name, last_name, gender, address, email)
VALUES ('
$first_name
', '
$last_name
', '
$gender
', '
$address
', '
$email
')"
;
if
(
$conn
->
query
(
$sql
)
===
TRUE
)
{
echo
"New record created successfully"
;
}
else
{
echo
"Error: "
.
$sql
.
"<br>"
.
$conn
->
error
;
}
// Close the connection
$conn
->
close
();
?>
Â
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, and Email Address
.
Data stored in the
client-side
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
Use HTML
<form>
elements to collect user data and submit it to PHP for processing.
Use PHP’s
mysqli_connect()
or
PDO
to establish a connection with the MySQL database.
Sanitize form input and use SQL queries (preferably prepared statements) to insert data into the database.
Check for connection or query errors and display appropriate messages to the user.
Conclusion
Inserting form data into a MySQL database using PHP is a fundamental process for building dynamic web applications. By using proper validation, prepared statements, and secure connections, you can ensure that user data is safely stored. This process enhances both functionality and user experience, making your web application more reliable and scalable. |
| Markdown | [](https://www.geeksforgeeks.org/)

- Sign In
- [Courses]()
- [Tutorials]()
- [Interview Prep]()
- [PHP Tutorial](https://www.geeksforgeeks.org/php/php-tutorial/)
- [PHP Exercises](https://www.geeksforgeeks.org/php/php-exercises-practice-questions-and-solutions/)
- [PHP Array](https://www.geeksforgeeks.org/php/php-array-functions-complete-reference/)
- [PHP String](https://www.geeksforgeeks.org/php/php-string-functions-complete-reference/)
- [PHP Calendar](https://www.geeksforgeeks.org/php/php-calendar-functions-complete-reference/)
- [PHP Filesystem](https://www.geeksforgeeks.org/php/php-filesystem-functions-complete-reference/)
- [PHP Math](https://www.geeksforgeeks.org/php/php-math-functions-complete-reference/)
- [PHP Programs](https://www.geeksforgeeks.org/php/php-examples/)
- [PHP Array Programs](https://www.geeksforgeeks.org/php/php-array-programs/)
- [PHP String Programs](https://www.geeksforgeeks.org/php/php-string-programs/)
# How to Insert Form Data into Database using PHP ?
Last Updated : 23 Jul, 2025
Inserting form data into a MySQL database using PHP allows web applications to store user inputs like registrations or feedback. By collecting and validating the data from HTML forms, PHP securely inserts it into the database, ensuring data integrity and protection against security risks.
Here, we will see the complete process of inserting form data into a [MySQL](https://www.geeksforgeeks.org/mysql/mysql-tutorial/) database using [PHP](https://www.geeksforgeeks.org/php/php-tutorial/), from setting up the database to writing secure and efficient code.
### Requirements
Before you begin, ensure you have the following tools ready:
- A Web Server with [PHP](https://www.geeksforgeeks.org/php/php-tutorial/) Installed (e.g., [XAMPP](https://www.geeksforgeeks.org/php/xampp-full-form/), [WAMP](https://www.geeksforgeeks.org/installation-guide/how-to-install-and-set-up-a-wamp-server/), [MAMP](https://www.geeksforgeeks.org/installation-guide/how-to-install-and-set-up-a-wamp-server/?ref=rp))
- A database system to store the form data(e.g., [MySQL](https://www.geeksforgeeks.org/mysql/mysql-tutorial/) or [MariaDB](https://www.geeksforgeeks.org/dbms/introduction-of-mariadb/))
- A Working Code Editor (e.g., [VS Code](https://www.geeksforgeeks.org/installation-guide/how-to-install-visual-studio-code-on-windows/), [Sublime Text](https://www.geeksforgeeks.org/cpp/setting-up-sublime-text-for-cpp-competitive-programming-environment/))
### ****Quick Overview of HTML form****
An [HTML form](https://www.geeksforgeeks.org/html/html-forms/) is used to collect information from users on a website. It helps store data like usernames, passwords, contact numbers, and emails on a web server using interactive elements.
We use the [HTML \<Form\> Tag](https://www.geeksforgeeks.org/html/html-form-tag/) to create forms on our website. Below is the syntax:
```
<form> Form Elements... </form>
```
> ****Note:**** To pass the values to the next page, we use the page name with the following syntax. We can use either the GET or POST method to send data to the server.
```
<form action=other_page.php method= POST/GET>
Form Elements...
</form>
```
> ****Note:**** In PHP, we can connect to the database using the localhost [XAMPP web server](https://www.geeksforgeeks.org/dbms/performing-database-operations-in-xampp/). XAMPP stands for cross-platform, [Apache](https://www.geeksforgeeks.org/linux-unix/what-is-apache-server/), MySQL, PHP, and [Perl](https://www.geeksforgeeks.org/linux-unix/what-is-apache-server/). It is among the simple, lightweight****,**** local servers for website development.
## Insert Form Data into Database Using PHP (Step-by-Step)
### ****Step 1:**** Start the XAMPP Server
- Open the ****XAMPP Control Panel****.
- Start ****Apache**** (for PHP) and ****MySQL**** (for database).

### ****Step 2: Create the Database****
- Open `localhost/phpmyadmin` in your web browser.
- Create a new database and name it ****staff then c****lick ****Create****.

### ****Step 3: Create the Table****
- In the ****staff**** database, create a new table named ****college****.
- Add columns to the table (e.g., `first_name`, `last_name`, `gender`, `email`), then click ****Save****.

### ****Step 4: Establish a Database Connection in PHP****
- The database connection allows your PHP code to interact with MySQL, enabling data storage and retrieval.

- ****It**** refers to connecting your PHP code to a database so you can save and get data. This lets your website store things like user information and display it when needed.
****This is the PHP code to establish a connection with a MySQL database.****
PHP
``
### ****Step 5: Create PHP Files****
- Open a text editor (e.g., Notepad) and create two PHP files.
- ****index.php**** (for the form)
- ****insert.php**** (to insert form data into the database)

### ****Step 6: Write the PHP Code to Insert Data****
Here’s the code for the form and the code to insert data into the database:
****HTML Form (******`index.php`******):****
HTML
``
```
<!DOCTYPE html>
```
```
<html>
```
```
<head>
```
```
<title>Insert Page</title>
```
```
</head>
```
```
<body>
```
```
<center>
```
```
<h2>Storing Form Data in Database</h2>
```
```
<form action="insert.php" method="POST">
```
```
<!-- First Name input -->
```
```
First Name:
```
```
<input name="first_name" required type="text"/>
```
```
<br/><br/>
```
```
​
```
```
<!-- Last Name input -->
```
```
Last Name:
```
```
<input name="last_name" required type="text"/>
```
```
<br/><br/>
```
```
​
```
```
<!-- Gender selection -->
```
```
Gender:
```
```
<input name="gender" required type="radio" value="male"/> Male
```
```
<input name="gender" required type="radio" value="female"/> Female
```
```
<br/><br/>
```
```
​
```
```
<!-- Address input -->
```
```
Address:
```
```
<textarea name="address" required></textarea>
```
```
<br/><br/>
```
```
​
```
```
<!-- Email input -->
```
```
Email:
```
```
<input name="email" required type="email"/>
```
```
<br/><br/>
```
```
​
```
```
<!-- Submit button -->
```
```
<input type="submit" value="Submit"/>
```
```
</form>
```
```
</center>
```
```
</body>
```
```
</html>
```

****PHP Code to Insert Data into the Database (******`insert.php`******):****
PHP
``
> ****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, and Email Address****.

Data stored in the ****client-side**** 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****
- Use HTML `<form>` elements to collect user data and submit it to PHP for processing.
- Use PHP’s `mysqli_connect()` or `PDO` to establish a connection with the MySQL database.
- Sanitize form input and use SQL queries (preferably prepared statements) to insert data into the database.
- Check for connection or query errors and display appropriate messages to the user.
## Conclusion
Inserting form data into a MySQL database using PHP is a fundamental process for building dynamic web applications. By using proper validation, prepared statements, and secure connections, you can ensure that user data is safely stored. This process enhances both functionality and user experience, making your web application more reliable and scalable.
Comment
[S](https://www.geeksforgeeks.org/user/sravankumar_171fa07058/)
[sravankumar\_171fa07058](https://www.geeksforgeeks.org/user/sravankumar_171fa07058/)
Follow
75
Article Tags:
Article Tags:
[PHP](https://www.geeksforgeeks.org/category/web-technologies/php/)
[HTML-Misc](https://www.geeksforgeeks.org/tag/html-misc/)
[PHP-Misc](https://www.geeksforgeeks.org/tag/php-misc/)
### Explore
[](https://www.geeksforgeeks.org/)

Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)

Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
[](https://geeksforgeeksapp.page.link/gfg-app)[](https://geeksforgeeksapp.page.link/gfg-app)
- Company
- [About Us](https://www.geeksforgeeks.org/about/)
- [Legal](https://www.geeksforgeeks.org/legal/)
- [Privacy Policy](https://www.geeksforgeeks.org/legal/privacy-policy/)
- [Contact Us](https://www.geeksforgeeks.org/about/contact-us/)
- [Advertise with us](https://www.geeksforgeeks.org/advertise-with-us/)
- [GFG Corporate Solution](https://www.geeksforgeeks.org/gfg-corporate-solution/)
- [Campus Training Program](https://www.geeksforgeeks.org/campus-training-program/)
- Explore
- [POTD](https://www.geeksforgeeks.org/problem-of-the-day)
- [Job-A-Thon](https://practice.geeksforgeeks.org/events/rec/job-a-thon/)
- [Blogs](https://www.geeksforgeeks.org/category/blogs/?type=recent)
- [Nation Skill Up](https://www.geeksforgeeks.org/nation-skill-up/)
- Tutorials
- [Programming Languages](https://www.geeksforgeeks.org/computer-science-fundamentals/programming-language-tutorials/)
- [DSA](https://www.geeksforgeeks.org/dsa/dsa-tutorial-learn-data-structures-and-algorithms/)
- [Web Technology](https://www.geeksforgeeks.org/web-tech/web-technology/)
- [AI, ML & Data Science](https://www.geeksforgeeks.org/machine-learning/ai-ml-and-data-science-tutorial-learn-ai-ml-and-data-science/)
- [DevOps](https://www.geeksforgeeks.org/devops/devops-tutorial/)
- [CS Core Subjects](https://www.geeksforgeeks.org/gate/gate-exam-tutorial/)
- [Interview Preparation](https://www.geeksforgeeks.org/aptitude/interview-corner/)
- [Software and Tools](https://www.geeksforgeeks.org/websites-apps/software-and-tools-a-to-z-list/)
- Courses
- [ML and Data Science](https://www.geeksforgeeks.org/courses/category/machine-learning-data-science)
- [DSA and Placements](https://www.geeksforgeeks.org/courses/category/dsa-placements)
- [Web Development](https://www.geeksforgeeks.org/courses/category/development-testing)
- [Programming Languages](https://www.geeksforgeeks.org/courses/category/programming-languages)
- [DevOps & Cloud](https://www.geeksforgeeks.org/courses/category/cloud-devops)
- [GATE](https://www.geeksforgeeks.org/courses/category/gate)
- [Trending Technologies](https://www.geeksforgeeks.org/courses/category/trending-technologies/)
- Videos
- [DSA](https://www.geeksforgeeks.org/videos/category/sde-sheet/)
- [Python](https://www.geeksforgeeks.org/videos/category/python/)
- [Java](https://www.geeksforgeeks.org/videos/category/java-w6y5f4/)
- [C++](https://www.geeksforgeeks.org/videos/category/c/)
- [Web Development](https://www.geeksforgeeks.org/videos/category/web-development/)
- [Data Science](https://www.geeksforgeeks.org/videos/category/data-science/)
- [CS Subjects](https://www.geeksforgeeks.org/videos/category/cs-subjects/)
- Preparation Corner
- [Interview Corner](https://www.geeksforgeeks.org/interview-prep/interview-corner/)
- [Aptitude](https://www.geeksforgeeks.org/aptitude/aptitude-questions-and-answers/)
- [Puzzles](https://www.geeksforgeeks.org/aptitude/puzzles/)
- [GfG 160](https://www.geeksforgeeks.org/courses/gfg-160-series)
- [System Design](https://www.geeksforgeeks.org/system-design/system-design-tutorial/)
[@GeeksforGeeks, Sanchhaya Education Private Limited](https://www.geeksforgeeks.org/), [All rights reserved](https://www.geeksforgeeks.org/copyright-information/)
![]() |
| Readable Markdown | Last Updated : 23 Jul, 2025
Inserting form data into a MySQL database using PHP allows web applications to store user inputs like registrations or feedback. By collecting and validating the data from HTML forms, PHP securely inserts it into the database, ensuring data integrity and protection against security risks.
Here, we will see the complete process of inserting form data into a [MySQL](https://www.geeksforgeeks.org/mysql/mysql-tutorial/) database using [PHP](https://www.geeksforgeeks.org/php/php-tutorial/), from setting up the database to writing secure and efficient code.
### Requirements
Before you begin, ensure you have the following tools ready:
- A Web Server with [PHP](https://www.geeksforgeeks.org/php/php-tutorial/) Installed (e.g., [XAMPP](https://www.geeksforgeeks.org/php/xampp-full-form/), [WAMP](https://www.geeksforgeeks.org/installation-guide/how-to-install-and-set-up-a-wamp-server/), [MAMP](https://www.geeksforgeeks.org/installation-guide/how-to-install-and-set-up-a-wamp-server/?ref=rp))
- A database system to store the form data(e.g., [MySQL](https://www.geeksforgeeks.org/mysql/mysql-tutorial/) or [MariaDB](https://www.geeksforgeeks.org/dbms/introduction-of-mariadb/))
- A Working Code Editor (e.g., [VS Code](https://www.geeksforgeeks.org/installation-guide/how-to-install-visual-studio-code-on-windows/), [Sublime Text](https://www.geeksforgeeks.org/cpp/setting-up-sublime-text-for-cpp-competitive-programming-environment/))
### ****Quick Overview of HTML form****
An [HTML form](https://www.geeksforgeeks.org/html/html-forms/) is used to collect information from users on a website. It helps store data like usernames, passwords, contact numbers, and emails on a web server using interactive elements.
We use the [HTML \<Form\> Tag](https://www.geeksforgeeks.org/html/html-form-tag/) to create forms on our website. Below is the syntax:
```
<form> Form Elements... </form>
```
> ****Note:**** To pass the values to the next page, we use the page name with the following syntax. We can use either the GET or POST method to send data to the server.
```
<form action=other_page.php method= POST/GET>
Form Elements...
</form>
```
> ****Note:**** In PHP, we can connect to the database using the localhost [XAMPP web server](https://www.geeksforgeeks.org/dbms/performing-database-operations-in-xampp/). XAMPP stands for cross-platform, [Apache](https://www.geeksforgeeks.org/linux-unix/what-is-apache-server/), MySQL, PHP, and [Perl](https://www.geeksforgeeks.org/linux-unix/what-is-apache-server/). It is among the simple, lightweight****,**** local servers for website development.
## Insert Form Data into Database Using PHP (Step-by-Step)
### ****Step 1:**** Start the XAMPP Server
- Open the ****XAMPP Control Panel****.
- Start ****Apache**** (for PHP) and ****MySQL**** (for database).

### ****Step 2: Create the Database****
- Open `localhost/phpmyadmin` in your web browser.
- Create a new database and name it ****staff then c****lick ****Create****.

### ****Step 3: Create the Table****
- In the ****staff**** database, create a new table named ****college****.
- Add columns to the table (e.g., `first_name`, `last_name`, `gender`, `email`), then click ****Save****.

### ****Step 4: Establish a Database Connection in PHP****
- The database connection allows your PHP code to interact with MySQL, enabling data storage and retrieval.

- ****It**** refers to connecting your PHP code to a database so you can save and get data. This lets your website store things like user information and display it when needed.
****This is the PHP code to establish a connection with a MySQL database.****
``
### ****Step 5: Create PHP Files****
- Open a text editor (e.g., Notepad) and create two PHP files.
- ****index.php**** (for the form)
- ****insert.php**** (to insert form data into the database)

### ****Step 6: Write the PHP Code to Insert Data****
Here’s the code for the form and the code to insert data into the database:
****HTML Form (******`index.php`******):****

****PHP Code to Insert Data into the Database (******`insert.php`******):****
``
> ****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, and Email Address****.

Data stored in the ****client-side**** 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****
- Use HTML `<form>` elements to collect user data and submit it to PHP for processing.
- Use PHP’s `mysqli_connect()` or `PDO` to establish a connection with the MySQL database.
- Sanitize form input and use SQL queries (preferably prepared statements) to insert data into the database.
- Check for connection or query errors and display appropriate messages to the user.
## Conclusion
Inserting form data into a MySQL database using PHP is a fundamental process for building dynamic web applications. By using proper validation, prepared statements, and secure connections, you can ensure that user data is safely stored. This process enhances both functionality and user experience, making your web application more reliable and scalable. |
| Shard | 103 (laksa) |
| Root Hash | 12046344915360636903 |
| Unparsed URL | org,geeksforgeeks!www,/php/how-to-insert-form-data-into-database-using-php/ s443 |