Current Location: Home> Latest Articles> How to Insert Data into a Database Using PHP: A Complete Tutorial

How to Insert Data into a Database Using PHP: A Complete Tutorial

gitbox 2025-06-26

PHP is a widely used scripting language in web development. In PHP development, database operations are very common, and inserting data into a database is one of the most basic and frequent tasks. This article will show you how to perform this operation using PHP.

1. Connect to the Database

First, we need to connect to the database in PHP. You can use PHP's mysqli or PDO extensions to establish the connection. Below is an example code to connect to a database using the mysqli extension:


$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

Please make sure to replace `your_username`, `your_password`, and `your_database_name` with your actual database username, password, and database name.

2. Build the SQL Insert Query

Next, we need to construct the SQL insert query to insert data into the database. When building the query, we must specify the table name, fields, and corresponding values. Here’s an example of an SQL insert query:


$sql = "INSERT INTO your_table_name (field1, field2, field3)
VALUES ('value1', 'value2', 'value3')";

In this example, replace `your_table_name` with your actual table name, `field1`, `field2`, and `field3` with your field names, and `value1`, `value2`, and `value3` with the values you want to insert.

3. Execute the SQL Insert Query

Finally, we need to execute the SQL insert query to insert the data into the database. You can use the `query()` method of mysqli to execute this SQL statement. Below is an example of executing the insert query:


if ($conn->query($sql) === TRUE) {
    echo "Successfully inserted one record";
} else {
    echo "Error inserting record: " . $conn->error;
}

If the data is inserted successfully, the program will output "Successfully inserted one record"; otherwise, it will display an error message.

4. Full Example Code

Here is the complete example code showing how to insert a record into the database using PHP:


$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO your_table_name (field1, field2, field3)
VALUES ('value1', 'value2', 'value3')";
if ($conn->query($sql) === TRUE) {
    echo "Successfully inserted one record";
} else {
    echo "Error inserting record: " . $conn->error;
}
$conn->close();

Conclusion

By following the steps outlined in this article, you should now know how to insert a record into a database using PHP. First, connect to the database and check if the connection is successful. Then, build the appropriate SQL insert query and execute it to insert the data. By following these steps, you can easily implement the data insertion functionality in your PHP applications.