0% found this document useful (0 votes)
4 views9 pages

How To Insert Form Data Into Database Using PHP

The document provides a step-by-step guide on how to insert form data into a MySQL database using PHP, covering the necessary tools, database setup, and PHP code required for the process. It emphasizes the importance of data validation and security measures to protect against risks. The conclusion highlights that this process is essential for building dynamic web applications that enhance user experience.

Uploaded by

monikamr.it
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

How To Insert Form Data Into Database Using PHP

The document provides a step-by-step guide on how to insert form data into a MySQL database using PHP, covering the necessary tools, database setup, and PHP code required for the process. It emphasizes the importance of data validation and security measures to protect against risks. The conclusion highlights that this process is essential for building dynamic web applications that enhance user experience.

Uploaded by

monikamr.it
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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 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
click 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.
 [Link] (for the form)
 [Link] (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 ([Link]):
<!DOCTYPE html>
<html>
<head>
<title>Insert Page</title>
</head>
<body>
<center>
<h2>Storing Form Data in Database</h2>
<form action="[Link]" 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
([Link]):
<?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/[Link] 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.

You might also like