How to Insert Data MySQL Database Using PHP

In this tutorial how to insert data in a MySQL table using PHP.

You can do this with the INSERT INTO statement. There are three ways we can write this.

  1. Procedural Method
  2. Object Oriented Method
  3. PDO Method

We will discuss all the methods here. You can use any one.


We will discuss Procedural Method. Example is given below.

First, we will create a database connection. $con is a variables. mysqli_connect() function is establish database connection. It is always connected localhost. This function requirement database user, Password and database name.

This upper PHP code is checking. Database connection has been made. If not, then it will give the error. mysqli_connect_error() is function give the error.

INSERT INTO is a PHP query. This PHP query insert value database table. MEMBER is a table name and name,add,email is a table Field name. $sql is a PHP variables. Database structure example is given below.

mysql database structure

This mysqli_query() execute INSERT INTO PHP query. This mysqli_query() can’t execute INSERT INTO PHP query. Show error “Could not able to execute”. It is if and else condition. Ending Close this connection. Full code given bellow.

How to use this PHP code in HTML

Creating the HTML Form

This is a simple HTML form. This form gives you three input.

Creating insert.php Page

When user clicks the submit button in the HTML form. The form data is sent to insert.php file. The insert.php file connect to the MySQL database server and insert value database member table.

The next tutorial insert data Object Oriented Method and PDO Method