MySQL Insert, Multiple Insert, and Last Inserted Query in PHP


MySQL Insert, Multiple Insert, and Last Inserted Query in PHP

Welcome to Part 3 of MySQL Tutorial for Beginners in 7 Days. This is a continuation of our previous tutorial about MySQL Create Table in Multiple Ways. In this MySQL tutorial will focus more on MySQL Insert, Multiple Insert, and Last Inserted Query.


MySQL Insert

MySQL Insert Query is used to insert a record in MySQL database the statement used is INSERT INTO command.


INSERT INTO name_of_table (column1, column2, column3,…)
VALUES (value1, value2, value3,…)

Rules in performing the INSERT INTO Command:

  • A Numeric Values and NULL word should not be quoted
  • String Values inside SQL query should be quoted
  • Using PHP all SQL query should be quoted
  • No Need to specify a column that is SET to AUTO_INCREMENT and TIMESTAMP

In the last tutorial called MySQL Create Table, we created a blank table named ‘tblpeople’. This time, we’re going to populate this table.

Here are the different examples of populating the ‘tblpeople’:

Example using MySQLi (Object-Orient):

Example using MySQLi (Procedural):

Example using PDO:

MySQL Multiple Insert

MySQL Multiple Insert Performs multiple queries in MySQL, you need to use the function called “mysqli_multi_query”. This function allows you to perform one or more queries against the database, you only have to separate the query using the semicolon.

Example using MySQLi(Object-Oriented):

Example using MySQLi(Procedural):

Example using PDO:

In the above lesson, we tackle how to insert a record in a MySQL database table. This time we’re going discuss MySQL Last Inserted to get the last Inserted ID from our table “tblpeople”. Using the AUTO_INCREMENT field, we are able to get the ID of the last inserted or updated record immediately.

Since we will be using the same code that we use from our previous topic called MySQL Insert we only need to add a single line of code for retrieving and echo the Last Inserted ID.

MySQL Last Inserted

Example Using MySQLi(Object-Oriented):

Example Using MySQLi(Procedural):

Example Using PDO:


In this tutorial, we tackled different ways on how to perform MySQL Insert, Multiple Insert, and Last Inserted query using PHP in different coding styles. In the next lesson, we will be practicing how Load records from MySQL Database to tables using MySQL Select Statement command.

Questions from the author

Which is better to use PHP/MySQLi or PHP/PDO for MySQL Insert query? Leave a Comment below for your answer.

Looking for more source code? Type your keyword here here!



Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.