Insert data ke database dengan php

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.

Contents

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 INTO table_name VALUES (value1,value2,………….)
4 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

INSERT INTO table_name VALUES (value1,value2,………….)
5 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

INSERT INTO table_name VALUES (value1,value2,………….)
6 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

INSERT INTO table_name VALUES (value1,value2,………….)
7 function specified in an
INSERT INTO table_name VALUES (value1,value2,………….)
8 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

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

Code:

mysqli_query($connection, $query)

It will take two parameters.

  • The
    $connection = mysqli_connect($server_name, $user_name, $password,$database_name)
    0 parameter specifies the connection details.
  • The
    $connection = mysqli_connect($server_name, $user_name, $password,$database_name)
    1 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

$connection = mysqli_connect($server_name, $user_name, $password,$database_name)
2 function.

6. Close the connection.

This is the last step where we have to close the connection by using the

$connection = mysqli_connect($server_name, $user_name, $password,$database_name)
3 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

$connection = mysqli_connect($server_name, $user_name, $password,$database_name)
4 under the
$connection = mysqli_connect($server_name, $user_name, $password,$database_name)
5 folder with the following contents in it.

Heads Up: If you use Linux, the htdocs folder will be under

$connection = mysqli_connect($server_name, $user_name, $password,$database_name)
6 directory. If you're on Windows, the htdocs will be usually in C:\xampp\ folder.

<?php
//specify the server name and here it is localhost
$server_name = "localhost";

//specify the username - here it is root
$user_name = "root";

//specify the password - it is empty
$password = "";

//specify the database name - "my_company"
$database_name = "my_company";

// Creating the connection by specifying the connection details
$connection = mysqli_connect($server_name, $user_name, $password,$database_name);

//sql query to insert a row into sales table
$query = "INSERT INTO sales (id,name,count) VALUES (1,'Cooking-Gas',40)";
if (mysqli_query($connection, $query)) {
  echo "record Inserted Successfully";
} else {
  echo "Error:" . mysqli_error($connection);
}

//close the connection
mysqli_close($connection);
?>

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.

Insert data ke database dengan php
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.

Insert data ke database dengan php
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 INTO table_name VALUES (value1,value2,………….)
4 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

$connection = mysqli_connect($server_name, $user_name, $password,$database_name)
8 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

INSERT INTO table_name VALUES (value1,value2,………….)
7 function specified in an
INSERT INTO table_name VALUES (value1,value2,………….)
8 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:

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

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

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

Code:

mysqli_query($connection, $query)

As you can see, It takes two parameters.

  • The
    $connection = mysqli_connect($server_name, $user_name, $password,$database_name)
    0 parameter specifies the connection details.
  • The
    $connection = mysqli_connect($server_name, $user_name, $password,$database_name)
    1 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

$connection = mysqli_connect($server_name, $user_name, $password,$database_name)
2 function.

6. Close the connection.

This is the last step where we have to close the connection by using the

$connection = mysqli_connect($server_name, $user_name, $password,$database_name)
3 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

$query = "INSERT INTO table_name (id,name,count) VALUES (1,'Cooking-Gas',40)";
6 under the
$connection = mysqli_connect($server_name, $user_name, $password,$database_name)
5 folder with the following contents in it.

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

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

Insert data ke database dengan php
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.

Insert data ke database dengan php
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

CodeInsert Data In MySQLInsert RecordsLinuxmacOSMariaDBMySQLMySQL CommandsPHPPHP MySQLphpMyAdminWindowsXAMPP

Bagaimana cara mengkoneksikan PHP dengan database MySQL?

Empat langkah menggunakan MySQLi untuk membuat koneksi database PHP ke MySQL:.
Buka File Manager -> public_html..
Buat File Baru dengan mengklik ikon tambah file pada menu di atas layar..
Simpan dengan nama databaseconnect.php, atau nama lain yang Anda inginkan, tapi ekstensinya tetap ..

Apa itu INSERT pada database?

Perintah INSERT digunakan untuk menambahkan baris data baru kepada suatu tabel dalam database. Perintah INSERT dapat menambahkan satu baris data baru dan dapat juga menambahkan beberapa baris data sekaligus.

Bagaimana cara import data pada phpMyAdmin?

Impor file SQL ke database MySQL.
Masuk ke phpMyAdmin..
Di phpMyAdmin, di menu sebelah kiri, pilih nama database yang ingin Anda gunakan. ... .
Di menu bagian atas, pilih Impor..
Gunakan Pilih file..
Temukan dan pilih file yang ingin Anda impor, lalu pilih Buka..
Di bagian bawah halaman, pilih Mulai..

Apa itu database pada PHP?

Database adalah kumpulan informasi yang disimpan di dalam komputer secara sistematik sehingga dapat diperiksa menggunakan suatu program komputer untuk memperoleh informasi dari basis data tersebut.