November 28, 2026
How to insert data in MySQL using PHP?
You can insert records in a table with insert query. You can write insert query in two ways.
First you can insert data in table without mentioning column names. In this form of insert query you have to include values for each column present in table. If you does not include value for any column then query will get failed and value will not be inserted in table. The syntax of insert query is mentioned below:
Syntax
INSERT INTO table_name
VALUES (value1, value2, value3,...);
There is second ways also present, in it you can insert data in table with mentioning both column names and values. In this form of insert query you does not need to include all the columns and values in your insert query to insert data in table. The syntax of insert query is mentioned below:
Syntax
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...);
For execution of insert query in PHP you need mysqli_query() function. Example is mentioned below:
Code
<?php
// Create database connection
$connection = mysqli_connect("localhost", "marcus", "xyz123", "my_db");
// Check connection
if (!$connection) {
die("Could not connect with MySQL: " . mysqli_connect_error());
}
// Insert into Employee table
$query1 = 'INSERT INTO Employee (FirstName, LastName, phoneNo)
VALUES ("Edge", "Boucher", "35457845")';
if (!mysqli_query($connection, $query1)) {
echo "Error inserting into Employee: " . mysqli_error($connection);
}
// Insert into Persons table
$query2 = 'INSERT INTO Persons
VALUES ("Kane", "Johnson", "33457845")';
if (!mysqli_query($connection, $query2)) {
echo "Error inserting into Persons: " . mysqli_error($connection);
}
// Close database connection
mysqli_close($connection);
?>
We have an example through this you can insert data from an HTML form to a database
There is an HTML form that can be used to add new records to the "Employee" table. The form is mentioned below:
Code
<html>
<body>
<form action="insert_record.php" method="post">
<table>
<tr>
<td colspan="2">Add Employee Details</td>
</tr>
<tr>
<td>Firstname:</td>
<td><input type="text" name="firstname" id="firstname" /></td>
</tr>
<tr>
<td>Lastname:</td>
<td><input type="text" name="lastname" id="lastname" /></td>
</tr>
<tr>
<td>Phone No:</td>
<td><input type="text" name="phone" id="phone" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Save" name="smtSave" id="smtSave"/>
</td>
</tr>
</table>
</form>
</body>
</html>
Whenever "Save" button will be clicked from the HTML Add Employee Details form then form's data will be sent to "insert_record.php" file. "insert_record.php" file is mentioned below. The "insert_record.php" file connects to a database and save form's data in Employee table.
Code
<?php
// Connect to MySQL database
$connection = mysqli_connect("localhost", "marcus", "xyz123", "my_db");
// Check connection
if (!$connection)
{
die("Could not connect with MySQL: " . mysqli_connect_error());
}
// Get values from POST
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$phone = $_POST["phone"];
// Prepare SQL query
$sql_query = "INSERT INTO Employee (FirstName, LastName, PhoneNo)
VALUES (?, ?, ?)";
$stmt = mysqli_prepare($connection, $sql_query);
if (!$stmt)
{
die("Error occurred: " . mysqli_error($connection));
}
// Bind parameters
mysqli_stmt_bind_param($stmt, "sss", $firstname, $lastname, $phone);
// Execute query
if (!mysqli_stmt_execute($stmt))
{
die("Error occurred: " . mysqli_stmt_error($stmt));
}
echo "Record added successfully.";
// Close statement and connection
mysqli_stmt_close($stmt);
mysqli_close($connection);
?>
FAQ
1. What is the difference between INSERT INTO with and without column names?
When you specify column names, you can insert values into selected columns. When you omit column names, you must provide values in the correct order for all columns that require values.
2. Which PHP function is used to insert data into MySQL?
You can use the mysqli_query() function to execute an INSERT query in PHP. Alternatively, you can use prepared statements with mysqli_prepare() and mysqli_stmt_execute().
3. How to insert data into MySQL using PHP from an HTML form?
Create an HTML form with input fields and set its method to POST. When the user submits the form, PHP receives the values through the $_POST array and inserts them into the MySQL database using an INSERT query.