WeCodeThings Library
Library of code bits
Glossary    Contact Us
Need a web designer? visit WeCodeThings.Com for more details
Search  
   
Browse by Category

WeCodeThings Library .: PHP .: PHP Simple Insert Into MySQL Database using isset function $_POST



PHP Simple Insert Into MySQL Database using isset function $_POST

To insert an item into the database, you can do a simple form and have it submit it to itself. When the submit call is made, it reloads the page. As the page reloads, the PHP IF statement looks for $_POST[‘btnSubmit’] and if it has been pressed, then it will execute the code to insert the data into the database.

If you are unsure how to open up a connection to the database, please go here:
PHP Simple MySQL Open Configure and Close the Connection to a Database


  1. <?php
  2. if(isset($_POST['btnSubmit']))
  3. {
  4. include 'config.php';
  5. include 'opendb.php';
  6.  
  7. $first = $_POST['txtFirst'];
  8. $last = $_POST['txtLast'];
  9.  
  10. $myquery = "INSERT INTO tblTest (tstFirstName, tstLastName)
  11.    VALUES ('$_POST[txtFirst]', '$_POST[txtLast]')";
  12.  
  13. mysql_query($myquery) or die('Error, insert query failed');  //order executes
  14.  
  15. include 'closedb.php';
  16. echo "New user added";
  17. }
  18. ?>
  19. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  20. <html xmlns="http://www.w3.org/1999/xhtml">
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  22. <title>php test</title>
  23. </head>
  24. <form action="" method="post" name="form1">
  25.   <td>
  26.    First Name:
  27.   </td>
  28.   <td>
  29.    <input type="text" name="txtFirst" />
  30.         </td>
  31.     </tr>
  32.      <td>
  33.          Last Name:
  34.         </td>
  35.         <td>
  36.          <input type="text" name="txtLast" />
  37.         </td>
  38.     </tr>
  39.     <tr>
  40.      <td colspan="2">&nbsp;
  41.      </td>
  42.     </tr>
  43.     <tr>
  44.      <td colspan="2">
  45.          <input type="submit" name="btnSubmit" id="btnSubmit" value="Insert Name" />
  46.         </td>
  47.     </tr>
  48. </table>
  49. </form>
  50. </body>


How helpful was this article to you?

Related Articles

article PHP Simple MySQL Open Configure and Close the Connection to a Database using PHP
Usually you want to create a “config.php”...

(No rating)  5-5-2009    Views: 756   
article mySQL SQL Select the last 50 rows inserted into the database by using LIMIT
In mySQL you can show a specific number of...

(No rating)  8-3-2009    Views: 686   
article ColdFusion Insert Data Statement Into a Table using ColdFusion CFC CFQueryParam
Be sure that when you type out the field...

  4-28-2009    Views: 1743   

User Comments

Add Comment
No comments have been posted.




.: Powered by Lore .:Code by WeCodeThings Website Design and Development