How to Insert Data into MySQL Database Using PHP

How to Insert Data into MySQL Database

After creating a database and its table, you have to insert data into MySQL database. The data is the required main record you put and use for your project.

To insert a record into the MySQL database, you need to first create MySQL query and execute it using the PHP functions. There are two alternatives to PHP functions you can use to insert data into MySQL databases.

  1. mysqli_query()
  2. PDO::__query()

The mysqli_query() is easy to use a function to use for your project if you want to use only the MySQL database. If you want to use another database in future, you can use PDO function as it supports more than twelve different types of databases.

Inserting Data Using MySQLi Procedural
Inserting the data needs a query to create using the table fields and the values you want to put for them. You can follow the below-given examples to use for your projects.

The execution of the query is same as the others, only the query is different containing the insert query statement.

Inserting data using MySQLi Object-oriented
Using Object-oriented in your coding is the best practice to design and develop a project based on PHP. You can create the database connection, create your insertion query and execute it by using PHP object-oriented programming.