MySql Insert Into

The INSERT INTO statement is used to insert new records in a table.

Insert Data Into a Database Table

The INSERT INTO statement is used to add new records to a database table.

Syntax

It is possible to write the INSERT INTO statement in two forms.

The first form doesn’t specify the column names where the data will be inserted, only their values:


INSERT INTO table_name VALUES (value1, value2, value3,…)

The second form specifies both the column names and the values to be inserted:

INSERT INTO table_name (column1, column2, column3,...)VALUES (value1, value2, value3,...)

To get PHP to execute the statements above we must use the mysqli_query() function. This function is used to send a query or command to a MySQL connection.

Example

In the previous chapter we created a table named "emp", with three columns;"empid", "LastName""firstname""email""extension""officeCode"manager"Age" and "jobTitle""salary". We will use the same table in this example. The following example adds two new records to the "emp" table:

INSERT INTO `emp` (`empid`, `lastName`, `firstName`, `extension`, `email`, `officeCode`, `manager`, `jobTitle`, `salary`) VALUES
(1001, 'Murphy', 'Diane', 'x5800', 'abc@shiksha360.com', '1', NULL, 'President', '10000'),
(1002, 'Patterson', 'Mary', 'x4611', 'Patterson@shiksha360.com', '1', 1001, 'VP Sales', '10000'),
(1003, 'Firrelli', 'Jeff', 'x9273', 'Jeff@shiksha360.com', '1', 1002, 'VP Marketing', '12000'),
(1004, 'Patterson', 'William', 'x4871', 'William@shiksha360.com', '6', 1003, 'Sales Manager (APAC)', '13000'),
(1005, 'Bondur', 'Gerard', 'x5408', 'Gerard@shiksha360.com', '4', 1056, 'Sale Manager (EMEA)', '12000'),
(1006, 'Bow', 'Anthony', 'x5428', 'Anthony@shiksha360.com', '1', 1056, 'Sales Manager (NA)', '12000'),
(1007, 'Jennings', 'Leslie', 'x3291', 'Leslie@shiksha360.com', '1', 1143, 'Sales Rep', '24000'),
(1008, 'Thompson', 'Leslie', 'x4065', 'Leslie@shiksha360.com', '1', 1143, 'Sales Rep', '13000'),
(1009, 'Firrelli', 'Julie', 'x2173', 'Julie@shiksha360.com', '2', 1143, 'Sales Rep', '18000'),
(1010, 'Patterson', 'Steve', 'x4334', 'Steve@shiksha360.com', '2', 1143, 'Sales Rep', '12000'),
(1011, 'Tseng', 'Foon Yue', 'x2248', 'Foon Yue@shiksha360.com', '3', 1143, 'Sales Rep', '13000');

Admin has written 171 articles

Leave a Reply