Insert data to a MySQL table.

Source: Internet
Author: User
Tags configuration settings

Insert data to a MySQL table.

To INSERT data to a MySQL table, run the SQL INSERT INTO command. You can insert data into a MySQL table using a mysql> prompt or using any script, such as PHP.
Syntax:

The following is a general SQL syntax INSERT INTO command to INSERT data to a MySQL table:

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

The data type of the string to be inserted. All values need double or single quotation marks, for example,-"value ".
Insert data from the command prompt:

This will use the SQL INSERT INTO command to INSERT data to the MySQL table tutorials_tbl
Instance:

The following example creates three records to the tutorials_tbl table:

root@host# mysql -u root -p password;Enter password:*******mysql> use TUTORIALS;Database changedmysql> INSERT INTO tutorials_tbl    ->(tutorial_title, tutorial_author, submission_date)   ->VALUES   ->("Learn PHP", "John Poul", NOW());Query OK, 1 row affected (0.01 sec)mysql> INSERT INTO tutorials_tbl   ->(tutorial_title, tutorial_author, submission_date)   ->VALUES   ->("Learn MySQL", "Abdul S", NOW());Query OK, 1 row affected (0.01 sec)mysql> INSERT INTO tutorials_tbl   ->(tutorial_title, tutorial_author, submission_date)   ->VALUES   ->("JAVA Tutorial", "Sanjay", '2007-05-06');Query OK, 1 row affected (0.01 sec)mysql>

Note: All arrow symbols (->) are SQL commands that do not belong to them indicating a new line. They will automatically create a MySQL prompt, at the same time, press the Enter key to enter the semicolon that does not end in each line of command.

In the preceding example, tutorial_id is not provided because the AUTO_INCREMENT option is defined for this field during table creation. Therefore, MySQL is responsible for automatically inserting these IDs. NOW () is a MySQL function that returns the current date and time.
Insert data in PHP:

You can use the same SQL INSERT INTO command to INSERT data INTO a MySQL table using the PHP function mysql_query.
Example:

In this example, the user inserts three parameters into the MySQL table:

The best way to insert data is to use the get_magic_quotes_gpc () function to check the current configuration settings or reference the magic function. If the function returns false, use the addslashes () function to add a forward slash before quotation marks.

You can check a lot of the surrounding verification. If the input data is correct or not, you can take appropriate verification.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.