HERE

INSERT INTO table_name is the command that tells MySQL server to add a new row into a table named table_name. (column_1,column_2,…) specifies the columns to be updated in the new MySQL row VALUES (value_1,value_2,…) specifies the values to be added into the new row

When supplying the data values to be inserted into the new table, the following should be considered:

String data types – all the string values should be enclosed in single quotes. Numeric data types- all numeric values should be supplied directly without enclosing them in single or double-quotes. Date data types – enclose date values in single quotes in the format ‘YYYY-MM-DD’.

Suppose that we have the following list of new library members that need to be added to the database. Let’s INSERT data one by one. We will start with Leonard Hofstadter. We will treat the contact number as a numeric data type and not enclose the number in single quotes. Executing the above script drops the 0 from Leonard’s contact number. This is because the value will be treated as a numeric value, and the zero (0) at the beginning is dropped since it’s not significant. To avoid such problems, the value must be enclosed in single quotes as shown below – In the above case, zero(0) will not be dropped The query shown below demonstrates the above point. The above queries skipped the date of birth column. By default, MySQL will insert NULL values in columns that are omitted in the INSERT query. Let’s now insert the record for Leslie, which has the date of birth supplied. The date value should be enclosed in single quotes using the format ‘YYYY-MM-DD’. All of the above queries specified the columns and mapped them to values in the MySQL insert statement. If we are supplying values for ALL the columns in the table, then we can omit the columns from the MySQL insert query. Example:- Let’s now use the SELECT statement to view all the rows in the member’s table. Notice the contact number for Leonard Hofstadter has dropped the zero (0) from the contact number. The other contact numbers have not dropped the zero (0) at the beginning.

Inserting into a Table from another Table

The INSERT command can also be used to insert data into a table from another table. The basic syntax is as shown below. Let’s now look at a practical example. We will create a dummy table for movie categories for demonstration purposes. We will call the new categories table categories_archive. The script shown below creates the table. Execute the above script to create the table. Let’s now insert all the rows from the categories table into the categories archive table. The script shown below helps us to achieve that. Executing the above script inserts all the rows from the categories table into the categories archive table. Note the table structures will have to be the same for the script to work. A more robust script is one that maps the column names in the insert table to the ones in the table containing the data. The query shown below demonstrates its usage. Executing the SELECT query gives the following results shown below.

PHP Example: Insert into MySQL Table

The mysqli_query function is used to execute SQL queries. The SQL insert into table function can be used to execute the following query types:

Insert Select Update delete

It has the following syntax. HERE, “mysqli_query(…)” is the function that executes the SQL queries. “$query” is the SQL query to be executed “$link_identifier” is optional, it can be used to pass in the server connection link

Example

Summary

The INSERT command is used to add new data into a table. MySql will add a new row, once the command is executed. The date and string values should be enclosed in single quotes. The numeric values do not need to be enclosed in quotes. The INSERT command can also be used to insert data from one table into another.