MySQL insert data

 INSERT INTO  SQL statement is used to insert data into MySQL table .

Data can be inserted into the data table through the mysql> command prompt window, or through a PHP script.

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

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

If the data is of character type, single or double quotes must be used, such as "value".


Insert data via command prompt window

The following will use the SQL  INSERT INTO  statement to insert data into the MySQL data table runoob_tbl

The following example will insert three pieces of data into the runoob_tbl table:

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("Learn PHP","John Poul", NOW());
mysql> INSERT INTO runoob_tbl (runoob_title, runoob_author, submission_date) VALUES ("Learn MySQL","Abdul S", NOW());
mysql> INSERT INTO runoob_tbl (runoob_title, runoob_author, submission_date) VALUES ("JAVA Tutorial","Sanjay",'2007-05-06');
mysql>

In the above example, the data of runoob_id is not provided, because the field has been set as the AUTO_INCREMENT attribute when the table is created. So, this field will be incremented automatically without needing to be set. In the example NOW() is a MySQL function that returns a date and time.


Insert data using PHP script

You can use PHP's mysql_query() function to execute the  SQL INSERT INTO command to insert data.

The function has two parameters and returns true if the execution is successful, otherwise it returns false.

bool mysql_query( sql, connection );
Parameter Description
sql Required. Specifies the SQL query to send. NOTE: The query string should not end with a semicolon.
connection Optional. Specifies the SQL connection identifier. If not specified, the last open connection is used.

Example

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

  1. <html>
  2. <head>
  3. <metacharset="gb2312">
  4. <title>向 MySQL 数据库添加数据</title>
  5. </head>
  6. <body>
  7. <?php
  8. if(isset($_POST['add']))
  9. {
  10. $dbhost ='localhost:3036';
  11. $dbuser ='root';
  12. $dbpass ='rootpassword';
  13. $conn = mysql_connect($dbhost, $dbuser, $dbpass);
  14. if(! $conn )
  15. {
  16. die('Could not connect: '. mysql_error());
  17. }
  18. if(! get_magic_quotes_gpc())
  19. {
  20. $runoob_title = addslashes ($_POST['runoob_title']);
  21. $runoob_author = addslashes ($_POST['runoob_author']);
  22. }
  23. else
  24. {
  25. $runoob_title = $_POST['runoob_title'];
  26. $runoob_author = $_POST['runoob_author'];
  27. }
  28. $submission_date = $_POST['submission_date'];
  29. $sql ="INSERT INTO runoob_tbl ".
  30. "(runoob_title,runoob_author, submission_date) ".
  31. "VALUES ".
  32. "('$runoob_title','$runoob_author','$submission_date')";
  33. mysql_select_db('RUNOOB');
  34. $retval = mysql_query( $sql, $conn );
  35. if(! $retval )
  36. {
  37. die('Could not enter data: '. mysql_error());
  38. }
  39. echo "Entered data successfully\n";
  40. mysql_close($conn);
  41. }
  42. else
  43. {
  44. ?>
  45. <form method="post" action="<?php $_PHP_SELF ?>">
  46. <tablewidth="600"border="0"cellspacing="1"cellpadding="2">
  47. <tr>
  48. <tdwidth="250">Tutorial Title</td>
  49. <td>
  50. <inputname="runoob_title"type="text"id="runoob_title">
  51. </td>
  52. </tr>
  53. <tr>
  54. <tdwidth="250">Tutorial Author</td>
  55. <td>
  56. <inputname="runoob_author"type="text"id="runoob_author">
  57. </td>
  58. </tr>
  59. <tr>
  60. <tdwidth="250">Submission Date [ yyyy-mm-dd ]</td>
  61. <td>
  62. <inputname="submission_date"type="text"id="submission_date">
  63. </td>
  64. </tr>
  65. <tr>
  66. <tdwidth="250"></td>
  67. <td></td>
  68. </tr>
  69. <tr>
  70. <tdwidth="250"></td>
  71. <td>
  72. <inputname="add"type="submit"id="add"value="Add Tutorial">
  73. </td>
  74. </tr>
  75. </table>
  76. </form>
  77. <?php
  78. }
  79. ?>
  80. </body>
  81. </html>
operation result:


 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326562974&siteId=291194637