Inserting form data into MySQL table using PHP
This article will show you how to send data from an HTML form to a table in a database.
To do this, we will use the following steps:
- First, we'll create a basic HTML form to collect data.
-
We will then use
POST
the method to send the data from our HTML form to a table in our database.
sample tutorial
We have a table called in
our database parkinglot1
. Here is what the table looks like:
Suppose we have a new entry to add to the table. Our new entry is as follows, BrandName
for Ferrari
, OwnersName
for Alex
, RegistrationNumber
for KJG564R
.
How do we approach this problem? The first step is to create an HTML form to collect new data.
Create an HTML form to collect data
We will have three slots for inputting data and a submit button in our HTML form. We do not need to include CarID
the slots because PHP will automatically update the entries.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Form- Store Data</title>
</head>
<body>
<center>
<h1>This Form is For Storing Data for OUR Database</h1>
<form action="includes/insert.php" method="POST">
<p>
<label for="CarID">Car ID:</label>
<input type="text" name="CarID" id="CarID">
</p>
<p>
<label for="BrandName">Brand Name:</label>
<input type="text" name="BrandName" id="BrandName">
</p>
<p>
<label for="OwnersName">Owners Name:</label>
<input type="text" name="OwnersName" id="OwnersName">
</p>
<p>
<label for="RegistrationNumber">Registration Number:</label>
<input type="text" name="RegistrationNumber" id="RegistrationNum">
</p>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>
Output:
Now we can write the code to send data to our database.
Use POST
the method to send data from an HTML form to a table in a PHP database
<?php
$user = 'root';
$pass = '';
$db = 'sample tutorial';
//Replace 'sample tutorial' with the name of your database
$con = mysqli_connect("localhost", $user, $pass, $db);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$CarID = $_POST['CarID'];
$BrandName = $_POST['BrandName'];
$OwnersName = $_POST['OwnersName'];
$RegistrationNumber = $_POST['RegistrationNumber'];
$sql = "INSERT INTO parkinglot1 (CarID,BrandName,OwnersName, RegistrationNumber)
VALUES ('$CarID','$BrandName','$OwnersName', '$RegistrationNumber')";
mysqli_query($con, $sql);
header("Location: ../form.php"); // Return to where our form is stored
?>
header()
The function will return our HTML form, where we can fill in the values we want to insert, like this:
After submitting the data, the updated form should look like this:
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to select from multiple tables in MySQL
Publish Date:2025/04/25 Views:57 Category:MySQL
-
This article explains how to use MySQL to query from multiple tables in one script SELECT . Let's demonstrate a situation: SELECT name , price, details, type , FROM food, food_order WHERE breakfast.id = 'breakfast_id' Now, let's imagine FRO
Creating a table from CSV in MySQL
Publish Date:2025/04/25 Views:114 Category:MySQL
-
In this article, we aim to understand how to create a table from CSV in MySQL database. Businesses and organizations must quickly generate tables from large amounts of data. These organizations typically have large CSV files with large amou
Creating a Temporary Table in MySQL
Publish Date:2025/04/25 Views:183 Category:MySQL
-
In this article, we aim to explore different ways to create temporary tables in MySQL. One of the main features of temporary tables is that it helps in storing temporary data. This feature is enabled in MySQL 3.23 and later versions. These
Truncate all tables in Mysql
Publish Date:2025/04/25 Views:89 Category:MySQL
-
Today I will show you how to truncate all tables in Mysql. It is used when you want to delete the entire table TRUNCATE TABLE . TRUNCATE It is a type of DML statement, which means it cannot be rolled back once it is committed. There are two
Different ways to check if a row exists in a MySQL table
Publish Date:2025/04/25 Views:163 Category:MySQL
-
This article highlights different ways to check if a row exists in a MySQL table. We will use the EXISTS and NOT EXISTS operators. We can also use these two operators with IF() the function to get a meaningful message if a row (a record) is
Check if table exists in MySQL
Publish Date:2025/04/25 Views:194 Category:MySQL
-
This article provides several options to check if a table exists in MySQL. Before discussing it, let us first see what a table is in MySQL and when you need to check its existence. What is a table in MySQL? A table is a database object that
Rename columns in MySQL database
Publish Date:2025/04/25 Views:80 Category:MySQL
-
In this article, we aim to explore different ways to rename columns in MySQL. ALTER TABLE The command is mainly used to change the format of a given MySQL table. It can be used to add columns, change the data type within a column, delete co
Copying a table in MySQL
Publish Date:2025/04/25 Views:142 Category:MySQL
-
The purpose of this article is to explore different ways to create a copy of a table in MySQL. The source table is also called the table to be copied, and the target table is called the clone table, which can be from the same or a different
Get column names in MySQL
Publish Date:2025/04/25 Views:109 Category:MySQL
-
In this article, we aim to explore how to get the column names of a specific table in MySQL database. Often, when working with data in MySQL, we tend to forget the column names of a particular table in the database and the data types of dif