PHP Insert Data Into MySQL database
Insert data into MySQL using MySQLi and PDO
After creating the database and table, we can add data to the table.
Here are some syntax rules:
- SQL query statements in PHP must use quotes
- String values must be quoted in SQL query statements
- No quotes are needed for numerical values
- Quotation marks are not required for NULL values
The INSERT INTO statement is typically used to add new records to a MySQL table:
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...)
Learn more about SQL, check out our SQL tutorial .
In the previous chapters we have created the table "MyGuests". The table fields are: "id", "firstname", "lastname", "email" and "reg_date". Now let's start filling the table with data.
|Note: If the column is set to AUTO_INCREMENT (such as the "id" column) or TIMESTAMP (Such as the "reg_date" column), we don't need to specify a value in the SQL query; MySQL automatically adds a value to the column.|
The following example adds a new record to the "MyGuests" table: