Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MySQL Inserting Data

MySQL Inserting Data

The INSERT INTO statement is used to insert data into MySQL tables.

You can insert data into a table through the mysql> command prompt window, or through a PHP script.

Syntax

The following is the general ** INSERT INTO ** SQL syntax for inserting data into a MySQL table:



INSERT INTO table_name (column1, column2, column3, ...)

VALUES (value1, value2, value3, ...);

Parameter Description:

If the data is of character type, it must be enclosed in single quotes ' or double quotes ", such as: 'value1', "value1".

A simple example that inserts a row of data into a table named users:

INSERT INTO users (username, email, birthdate, is_active)

VALUES ('test', 'test@runoob.com', '1990-01-01', true);

If you want to insert data for all columns, you can omit the column names:



INSERT INTO users

VALUES (NULL,'test', 'test@runoob.com', '1990-01-01', true);

Here, NULL is a placeholder for the auto-increment column, indicating that the system will id generate a unique value for the column.

If you want to insert multiple rows of data, you can specify multiple sets of values in the VALUES clause:

INSERT INTO users (username, email, birthdate, is_active)

VALUES

    ('test1', 'test1@runoob.com', '1985-07-10', true),

    ('test2', 'test2@runoob.com', '1988-11-25', false),

    ('test3', 'test3@runoob.com', '1993-05-03', true);

The above code will insert three rows of data into the users table.


Inserting Data Through the Command Prompt Window

Below we will use the INSERT INTO statement to insert data into the MySQL table runoob_tbl

Example

In the following example we will insert three records into the runoob_tbl table:

Example

root@host# mysql

u root

p password;

Enter password:


mysql

USE

RUNOOB;

DATABASE

changed

mysql

INSERT

INTO

runoob_tbl

   

->

(

runoob_title

,

runoob_author

,

submission_date

)

   

->

VALUES

   

->

(

"Learning PHP"

,

"Runoob Tutorial"

,

NOW

(

)

)

;

Query OK

,

1

ROWS

affected

,

1

warnings

(

0.01

sec

)

mysql

INSERT

INTO

runoob_tbl

   

->

(

runoob_title

,

runoob_author

,

submission_date

)

   

->

VALUES

   

->

(

"Learning MySQL"

,

"Runoob Tutorial"

,

NOW

(

)

)

;

Query OK

,

1

ROWS

affected

,

1

warnings

(

0.01

sec

)

mysql

INSERT

INTO

runoob_tbl

   

->

(

runoob_title

,

runoob_author

,

submission_date

)

   

->

VALUES

   

->

(

"JAVA Tutorial"

,

"RUNOOB.COM"

,

'2016-05-06'

)

;

Query OK

,

1

ROWS

affected

(

0.00

sec

)

mysql

Note: The arrow marker -> is not part of the SQL statement; it merely indicates a new line. If an SQL statement is too long, we can create a new line with the Enter key to write the SQL statement. The command terminator of an SQL statement is the semicolon ;.

In the above example, we did not provide the data for runoob_id because we already set this field to AUTO_INCREMENT(auto-increment) attribute when creating the table. Therefore, this field will increment automatically without us needing to set it. In the example, NOW() is a MySQL function that returns the date and time.

Next we can view the data in the table with the following statement:

Read the data table:

select

from

runoob_tbl

;

Output result:


Inserting Data Using a PHP Script

You can use PHP's mysqli_query() function to execute the INSERT INTOcommand to insert data.

This function takes two parameters and returns TRUE on success, or FALSE otherwise.

Syntax



mysqli_query(connection,query,resultmode);

Parameters Description
connection Required. Specifies the MySQL connection to use.
query Required, specifies the query string.
resultmode

Optional. A constant. Can be any of the following values:

  • MYSQLI_USE_RESULT (use this if you need to retrieve large amounts of data)
  • MYSQLI_STORE_RESULT (default)
  • |

    Example

    In the following example, the program receives three fields of data entered by the user and inserts them into the table:

    Add data

    ' ; // Set encoding to prevent character garbling mysqli_query ( $conn , " set names utf8 " ) ; $runoob_title = ' Learning Python ' ; $runoob_author = ' RUNOOB.COM ' ; $submission_date = ' 2016-03-06 ' ; $sql = " INSERT INTO runoob_tbl " . " (runoob_title,runoob_author, submission_date) " . " VALUES " . " (' $runoob_title ',' $runoob_author ',' $submission_date ') " ; mysqli_select_db ( $conn , ' RUNOOB ' ) ; $retval = mysqli_query ( $conn , $sql ) ; if ( ! $retval ) { die ( ' Unable to insert data: ' . mysqli_error ( $conn ) ) ; } echo " Data inserted successfully \n " ; mysqli_close ( $conn ) ; ?>

    For inserting data containing Chinese, you need to add the mysqli_query($conn , "set names utf8"); statement.

    Next we can view the data in the table with the following statement:

    Read the data table:

    select

    from

    runoob_tbl

    ;

    Output result: