MySQL – Insert Query

To insert data into a MySQL table, you would need to use the SQL INSERT INTO command. You can insert data into the MySQL table by using the mysql> prompt or by using any script like PHP.

Syntax

Here is a generic SQL syntax of INSERT INTO command to insert data into the MySQL table −

INSERT INTO table_name ( field1, field2,...fieldN )
VALUES
( value1, value2,...valueN );

To insert string data types, it is required to keep all the values into double or single quotes. For example "value".

Inserting Data from the Command Prompt

To insert data from the command prompt, we will use SQL INSERT INTO command to insert data into MySQL table tutorials_tbl.

Example

The following example will create 3 records into employees table −

root@host# mysql -u root -p password;
Enter password:
mysql> use mydb;
Database changed
mysql> INSERT INTO `employees` (`id`, `company`, `first_name`, `last_name`,`job_title`, `mobile_phone`,`email_address`) VALUES (1, 'Northwind Traders', 'Nancy','kotas','developer','(123)555-0100','nancy@northwindtraders.com');
Query OK, 1 row affected (0.07 sec))
mysql> INSERT INTO `employees` (`id`, `company`, `first_name`, `last_name`,`job_title`, `mobile_phone`,`email_address`) VALUES (2, 'Northwind Traders', 'Cencini', 'Andrew','Vice President, Sales', '(123)555-0100', 'andrew@northwindtraders.com');
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO `employees` (`id`, `company`, `first_name`, `last_name`,`job_title`, `mobile_phone`,`email_address`) VALUES (3, 'Northwind Traders', 'Mariya', 'Jan','Sales Representative', '(123)555-0100', 'mariya@northwindtraders.com');
Query OK, 1 row affected (0.07 sec)

NOTE − Please note that all the arrow signs (->) are not a part of the SQL command. They are indicating a new line and they are created automatically by the MySQL prompt while pressing the enter key without giving a semicolon at the end of each line of the command.
In the above example, we have not provided a id because at the time of table creation, we had given AUTO_INCREMENT option for this field. So MySQL takes care of inserting these IDs automatically. Here, NOW() is a MySQL function, which returns the current date and time.

Inserting Data Using a PHP Script

You can use the same SQL INSERT INTO command into the PHP function mysqli_query() to insert data into a MySQL table.

Example

This example will take six parameters from the user and will insert them into the MySQL table −

alert('New record created successfully')
<?php
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
<title>Insrting values of Table in Mysql</title>
<table width="600" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="250">Company Name</td>
<td>
</td>
</tr>
<tr>
<td width="250">First Name</td>
<td>
</td>
</tr>
<tr>
<td width="250">Last Name</td>
<td> </td>
</tr>
<tr>
<td width="250">Job Title</td>
<td> </td>
</tr>
<tr>
<td width="250">Mobile no.</td>
<td> </td>
</tr>
<tr>
<td width="250">Email Address</td>
<td> </td>
</tr>
<tr>
<td width="250"></td>
<td>
</td>
</tr>
</table>

You can put many validations around to check if the entered data is correct or not and can take the appropriate action.

Drop MySQL Tables (Prev Lesson)
(Next Lesson) MySQL – UPDATE Query