A detailed tutorial on inserting data into a Mysql table _mysql

Source: Internet
Author: User
Tags configuration settings php language learn php

Inserting data into the MySQL table requires that you use the SQL INSERT INTO command. You can insert data into the MySQL table using the mysql> prompt or use any script, such as PHP.

The following is a generic SQL syntax insert INTO command to insert data into the MySQL table:

INSERT into table_name (field1, Field2,... fieldn)
            (value1, value2,... Valuen);

The string data type to insert, all values require double or single quotes, such as:-"value".
To insert data from a command prompt:

This inserts the data into the MySQL table using the SQL INSERT INTO command tutorials_tbl

The following example creates 3 entries to the TUTORIALS_TBL table:

root@host# mysql-u root-p password;
Enter password:*******
mysql> use tutorials;
Database changed
mysql> INSERT into Tutorials_tbl 
   -> (Tutorial_title, Tutorial_author, Submission_date)
   -> ("Learn PHP", "John Poul", now ());
Query OK, 1 row affected (0.01 sec)
mysql> INSERT into Tutorials_tbl
   -> (Tutorial_title, Tutorial_author, S ubmission_date)
   -> ("Learn MySQL", "Abdul S", now ());
Query OK, 1 row affected (0.01 sec)
mysql> INSERT into Tutorials_tbl
   -> (Tutorial_title, Tutorial_author, submission_date)
   -> ("JAVA Tutorial", "Sanjay", ' 2007-05-06 ');
Query OK, 1 row affected (0.01 sec)

NOTE: All arrow symbols (->) are not part of the SQL command they represent a new row, they will automatically create a MySQL prompt, press ENTER at the same time there is no semicolon at the end of each line command.

In the above example, we did not provide tutorial_id because when you create a table, you define a auto_increment option for this field. So MySQL is responsible for automatically inserting these IDs. Here now () is a MySQL function that returns the current date and time.
To insert data using the PHP language:

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

This example will take the user from three parameters and insert them into the MySQL table:


Data insertion Its best practice is to use function get_magic_quotes_gpc () to check current configuration settings or references to magic functions. If the function returns False, the function addslashes () is used before the slash quotation mark is added.

You can take a lot of validation checks around, and if you enter data correctly or not, you can use appropriate validation.

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.