Coding php insert data into database

After creating a MySQL database and table, we can start inserting the data (i.e. records) in them. In this tutorial, we are going to learn how to insert data in MySQL database using PHP in XAMPP stack.

Prerequisites

Make sure you have setup XAMPP stack in your system. The following guide explains how to setup XAMPP stack in Linux.

  • How To Install XAMPP In Linux

Alternatively, you can use the LAMP or LEMP stacks which provide both PHP and MySQL. If you're on Linux, refer to the following guides to install LAMP/LEMP stacks.

  • Install Apache, MySQL, PHP (LAMP Stack) On Ubuntu 20.04 LTS
  • Install Nginx, MySQL, PHP (LEMP Stack) On Ubuntu 20.04 LTS
  • Install Apache, MariaDB, PHP (LAMP Stack) In CentOS 8
  • Install Apache, MariaDB, PHP (LAMP) stack on Arch Linux
  • Install Nginx, MariaDB, PHP (LEMP) stack on Arch Linux

Setting up XAMPP is much easier than LAMP and LEMP stacks. So, we will will be using XAMPP stack throughout this guide.

After setting up the XAMPP stack, you need to create a MySQL database and table inside the database. Refer to the following guide to know how to create MySQL database and table in XAMPP stack.

  • Create MySQL Database And Table Using PHP In XAMPP

For demonstration purpose, I am going to create a table named "sales" in a database called "my_company" in my XAMPP stack.

Insert Data In MySQL Database Using PHP

We can insert a single record or multiple records at once in a MySQL table. First, we will see how to insert single record.

Inserting Single Record In MySQL Table Using PHP

We will use a SQL Query statement in our PHP code to insert a record in the table. We can insert a single record/row into a table by using the INSERT command.

Query Syntax:

INSERT INTO table_name(column1,column2,………….) VALUES (value1,value2,………….)  

(Or)

INSERT INTO table_name VALUES (value1,value2,………….)

Where, table_name represents the name of the table and columns represents the column name and values are inserted into the table.

Steps

1. Specify the servername, MySQL username, password and database name in the PHP code.

Here, the servername is localhost, username is root and password is empty. And the database name is my_company, we are creating a table called "sales" inside this database.

2. Create a connection using the above details.

By using my_sqli_connect() function, we will establish a connection. It will take three parameters. First will be the servername, second is the username and the last is password. It will also take a database name which is optional here, because we are just creating the connection.

Code:

$connection = mysqli_connect($server_name, $user_name, $password,$database_name)

3. Check the Connection.

We can check the connection using the mysqli_connect_error() function specified in an if condition. This function will display an error, if the connection is failed.

4. Specify the SQL Query to insert a single record into the table.

In this step, we specify the SQL query to insert values into the database. Let the database name is "my_company" and we are storing it in a variable named query. The table name is "sales" that has three columns.

Code:

$query = "INSERT INTO table_name (id,name,count) VALUES (1,'Cooking-Gas',40)";

5. Checking the creation.

If we want to check the values inserted or not, we can use the mysqli_query() function.

Code:

mysqli_query($connection, $query)

It will take two parameters.

  • The $connection parameter specifies the connection details.
  • The $query parameter is the sql query that we use to insert a record/row in the table

If this condition is True, then a row will be inserted. Otherwise it will return an error. We can check the error by using the mysqli_error($connection) function.

6. Close the connection.

This is the last step where we have to close the connection by using the mysqli_close() function.

Code:

mysqli_close($connection);

Now let us write a sample PHP code based on the above steps.

PHP Code To Insert A Single Record In MySQL Table

Create a new file named insert.php under the /htdocs folder with the following contents in it.

Heads Up: If you use Linux, the htdocs folder will be under /opt/lampp/ directory. If you're on Windows, the htdocs will be usually in C:\xampp\ folder.

In the above code, "my_company" is the database name and "sales" in the table name.

Open your web browser and navigate to http://localhost/insert.php. You will see an output like this in your browser window.

Coding php insert data into database
Insert A Single Record In MySQl Table

You can also verify if the record/row is inserted from phpMyAdmin as well. To do so, open phpMyAdmin dashboard by navigating to http://localhost/phpmyadmin URL from the browser.

Go to my_company -> sales and click Browse to see the inserted record.

Coding php insert data into database
View Records In MySQL Table

As you can see, we have successfully inserted a single record in the table called "sales" in the my_company database.

Inserting Multiple Records In MySQL Table Using PHP

We can insert multiple Records/Rows into a table by using the INSERT command.

Query Syntax:

INSERT INTO table_name(column1,column2,………….) VALUES (value1,value2,………….)

(Or)

INSERT INTO table_name VALUES (value1,value2,………….)

Where, table_name represents the name of the MySQL table and columns represents the column name and values are inserted into the table.

Steps

1. Specify servername, MySQL username, password and database name.

Here, the servername is localhost, username is root and password is empty. And the database name is "my_company", and we are creating a table inside this database.

2. Create a connection using the above details.

By using mysqli_connect() function, we will establish a connection. It will take three parameters. First will be the servername, second is the username and the last is the password. It will also take a database name which is optional here, because we are just creating the connection.

Code:

$connection = mysqli_connect($server_name, $user_name, $password,$database_name);

3. Check the Connection.

We can check the connection using mysqli_connect_error() function specified in an if condition. This function will represent an error, if the connection is failed.

4. Specify the SQL Query to insert multiple records into the table.

In this step, we specify the SQL query to insert values into the database in a variable. Let the database name be my_company and we are storing in a variable named query. The table name is sales that has three columns.

Code:

$query = "INSERT INTO table_name (id,name,count) VALUES (5,'Shampoo',10);";
$query .= "INSERT INTO table_name (id,name,count) VALUES (2,'Milk',20);";
$query .= "INSERT INTO table_name (id,name,count) VALUES (3,'Books',14);";
$query .= "INSERT INTO table_name (id,name,count) VALUES (4,'Chocos',45);";
$query .= "INSERT INTO table_name (id,name,count) VALUES (6,'Eggs',12)";

Heads Up: Please mind the dot (.) in second and the subsequent SQL queries. You need to put a dot (.) in front of the equal sign starting from second query.

5. Checking the creation.

If we want to check if the values inserted or not, we can use mysqli_query() function.

Code:

mysqli_query($connection, $query)

As you can see, It takes two parameters.

  • The $connection parameter specifies the connection details.
  • The $query parameter is the sql query that we use to insert multiple rows in the table

If this condition is True, then the rows will be inserted. Otherwise it will return an error. We can check the error by using the mysqli_error($connection) function.

6. Close the connection.

This is the last step where we have to close the connection by using the mysqli_close() function.

Code:

mysqli_close($connection);

Now let us write a sample PHP code to insert 5 records based on the above steps.

PHP Code To Insert Multiple Records In MySQL Table

Create a new file named insert_multiple.php under the /htdocs folder with the following contents in it.

Open your web browser and navigate to http://localhost/insert_multiple.php. You will see an output like this in your browser window.

Coding php insert data into database
Insert Multiple Records In MySQL Table

Let us verify if the records are actually inserted or not from the phpMyAdmin dashboard.

Open phpMyAdmin dashboard by navigating to http://localhost/phpmyadmin URL from the browser. Go to my_company-> sales and click on Browse button to see the inserted records.

Coding php insert data into database
View Records In MySQL Table Via PhpMyAdmin

Conclusion

In this tutorial, we discussed how to insert data in MySQL database using PHP code in XAMPP stack. We provided two sample PHP codes to demonstrate how to insert single and multiple records in a MySQL table.

Change the code as per your requirement and test them on your system. If you have any question, let us know via the comment section below.

Read Next:

  • How To Select Data From MySQL Database Using PHP in XAMPP Stack

How we can insert data in database using PHP?

The INSERT INTO statement is used to add new records to a MySQL table: INSERT INTO table_name (column1, column2, column3,...).
The SQL query must be quoted in PHP..
String values inside the SQL query must be quoted..
Numeric values must not be quoted..
The word NULL must not be quoted..

How do you add data to a database?

Simple INSERT statement to add data to the table. Use INSERT Statement to add multiple rows in the table. INSERT INTO SELECT clause to insert the output generated by the SELECT query. INSERT IGNORE clause to ignore the error generated during the execution of the query.

How can insert data in database in HTML?

For this you need to follow the following steps:.
Step 1: Filter your HTML form requirements for your contact us web page. ... .
Step 2: Create a database and a table in MySQL. ... .
Step 3: Create HTML form for connecting to database. ... .
Step 4: Create a PHP page to save data from HTML form to your MySQL database. ... .
Step 5: All done!.

What is insert into in PHP?

In PHP, we use the INSERT INTO statement to add new rows to a database table. By passing mysqli query to PHP, we can run the insert query(). We use mysqli multi query to execute multiple queries in a single call because mysqli query cannot execute multiple queries to avoid SQL injections.