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.
Syntax:
The following is a generic SQL syntax insert INTO command to insert data into the MySQL table:
INSERT into table_name (field1, Field2,... fieldn)
VALUES
(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
Instance:
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)
->values
-> ("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)
->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 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.
Example:
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.