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.