ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

SQL INSERT INTO Statement

sqltuts

A user can insert the data into MySQL table using SQL INSERT INTO command. It can be done in two ways:-

 

1.) By using mysql> prompt

2.) By using any script like PHP.

 

Basic format to insert the data into the table:

  1. INSERT INTO table_name ( data type1, data type2,...field data typeN )
  2.                        VALUES
  3.                        ( value1, value2,...valueN );

In order to have insert string data types, it is important to keep the values into the double or single quote, for example- “value”.

Inserting Data from Command Prompt:

This will use SQL INSERT INTO command to insert data into MySQL table tutorials_table.

Example:

Following example will create 3 records into tutorials_table table:

  1. root@host# mysql -u root -p password;
  2. Enter password:*******
  3. mysql> use Database tutorials;
  4. Database changed
  5. mysql> INSERT INTO Database  tutorials_table
  6.      ->( Database tutorial_chapter name, Database tutorial_prize, Database tutorial_fee)
  7.      ->VALUES
  8.      ->("CSS", 2000, 500);
  9. Query OK, 1 row affected (0.01 sec)
  10. mysql> INSERT INTO tutorials_tbl
  11. ->( Database tutorial_chapter name, Database tutorial_prize, Database tutorial_fee)
  12.      ->VALUES
  13.      ->("CSS", 50000, 1000);
  14. Query OK, 1 row affected (0.01 sec)
  15. mysql> INSERT INTO tutorials_tbl
  16.      ->( Database tutorial_chapter name, Database tutorial_prize, Database tutorial_fee)
  17.      ->VALUES
  18.      ->("HTML", 85566, 5000);
  19.  
  20. Query OK, 1 row affected (0.01 sec)
  21. mysql>

 

Inserting Data Using PHP Script:

SQL INSERT INTO command into PHP function mysql_query() to insert data into a MySQL table.

Example:

The following example takes three different choices from user and will insert them into MySQL table:

  1. <html>
  2. <head>
  3. <title> MySQL Database</title>
  4. </head>
  5. <body>
  6. <?php
  7. if(isset($_POST&#91;'sum']))
  8. {
  9. $dbhost = 'localhost';
  10. $dbusername = 'root';
  11. $dbpassword = 'rootpassword';
  12. $con = mysql_connect($dbhost, $dbusername, $dbpassword);
  13. if(! $con )
  14. {
  15.   die('Could not connect: ' . mysql_error());
  16. }
  17.  
  18. if(! get_support() )
  19. {
  20.    $Database tutorial_title = addslashes ($_POST&#91;'Databasetutorial_title']);
  21.   $Database tutorial_author = addslashes ($_POST&#91;'Database tutorial_prize']);
  22. }
  23. else
  24. {
  25.  $Databasetutorial_title = $_POST&#91;'tutorial_title'];
  26.   $Databasetutorial_prize = $_POST&#91;'tutorial_prize'];
  27. }
  28. $Databasesubmission_fee = $_POST&#91;'submission_fee'];
  29.  
  30. $sql = "INSERT INTO tutorials_table ".
  31.        "(tutorial_title,tutorial_prize, submission_fee) ".
  32.        "VALUES ".
  33.        "('$tutorial_title','$tutorial_prize','$submission_fee')";
  34. mysql_select_db('Database tutorials');
  35. $retval = mysql_query( $sql, $con );
  36. if(! $result )
  37. {
  38.   die('Could not enter data: ' . mysql_error());
  39. }
  40. echo "Entered data successfully\n";
  41. }
  42. else
  43. {
  44. ?>
  45. <form method="post" action="<?php $_PHP_SELF ?>">
  46. <table width="30" border="0" cellspacing="3" cellpadding="5">
  47. <tr>
  48. <td width="200">Database Tutorial</td>
  49. <td>
  50. <input name=" Database tutorial" type="text" id=" Database tutorial">
  51. </td>
  52. </tr>
  53. <tr>
  54. <td width="200">Tutorial FEE</td>
  55. <td>
  56. <input name="tutorial_FEE" type="text" id="tutorial_FEE">
  57. </td>
  58. </tr>
  59. <tr>
  60. <td width="200">Submission Date [ yyyy-mm-dd ]</td>
  61. <td>
  62. <input name="submission_FEE" type="text" id="submission_FEE">
  63. </td>
  64. </tr>
  65. <tr>
  66. <td width="200"> </td>
  67. <td> </td>
  68. </tr>
  69. <tr>
  70. <td width="200"> </td>
  71. <td>
  72. <input name="fee" type="submit" id="fee" value="Add fee">
  73. </td>
  74. </tr>
  75. </table>
  76. </form>
  77. <?php
  78. }
  79. ?>
  80. </body>
  81. </html>
Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!