The INSERT INTO statement in MySQL is used to insert new records into a table. You can insert data for specific columns or for all columns of a table. This is one of the most commonly used SQL operations in database-driven PHP applications.
The basic syntax for inserting data into a table is:
-- Basic INSERT syntax
INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);
This example inserts a new student record:
-- Insert values into all specified columns
INSERT INTO students (name, age, grade)
VALUES ('John Doe', 21, 'B');
A new row is added to the students table with the provided values. If the query executes successfully, MySQL returns a confirmation that one row was affected.
You can insert values into selected columns and allow others to use default values:
-- Insert data while skipping columns with defaults
INSERT INTO students (name, grade)
VALUES ('Alice Smith', 'A');
The diagram below represents how a PHP application sends an INSERT query to MySQL:
// PHP sends SQL query to MySQL server
User Form ➜ PHP Script ➜ MySQL INSERT ➜ Data Stored
-- Insert multiple rows in one query
INSERT INTO students (name, age, grade)
VALUES ('John Doe', 21, 'B'), ('Jane Doe', 22, 'A');
books table with title, author, price, and publication_date