PHP Inserting Data To MySQL

Submitted by: 
Language: 
Visitors have accessed this post 3325 times.

PHP Inserting Data To MySQL

After the tutorials in PHP MySQL Database and PHP Creating MySQL Tables that we made. So, let's start for adding data.

In this tutorial, we are going to learn on How To Create Insert Data To MySQL. For this tutorial, we have 3 ways to create this in PHP and MySQL. These are:

  • MySQLi (object-oriented)
  • MySQLi (procedural)
  • PDO (PHP Data Objects)

But, in this tutorial, we are going to use in PDO Syntax. This tutorial shows how to add data into a database.

In the previous tutorial, we create a database "myDatabase" and the table name is "tbl_registration". But, for this tutorial, our database name is "add_query_pdo" and the table name is "tbl_registration". So, we are going to insert data into the table.

Let’s start with:

Creating our Table

We are going to make our database.

To create a database:

  1. Open the PHPMyAdmin.
  2. Create a database and name it as "add_query_pdo".
  3. After creating a database name, then we are going to create our table. And name it as "tbl_registration".
  4. Kindly copy the code below.
  1. -- --------------------------------------------------------
  2.  
  3. --
  4. -- Table structure for table `tbl_registration`
  5. --
  6.  
  7. CREATE TABLE `tbl_registration` (
  8. `tbl_registration_id` INT(11) NOT NULL,
  9. `first_name` VARCHAR(100) NOT NULL,
  10. `middle_name` VARCHAR(100) NOT NULL,
  11. `last_name` VARCHAR(100) NOT NULL,
  12. `email` VARCHAR(100) NOT NULL,
  13. `contact_number` VARCHAR(100) NOT NULL
  14. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Creating Form Field

We have to create the form field where the user types the information in the textboxes.

  1. <form method="post" action="insert_query.php">
  2.  
  3. <h2>Insert Query Using PDO In PHP</h2>
  4. <table border="0" cellspacing="3" cellpadding="3" width="80%">
  5. <tr>
  6. <td><label>First Name</label></td>
  7. <td><input type="text" name="first_name" placeholder="First Name....." autofocus="autofocus"></td>
  8. </tr>
  9. <tr>
  10. <td><label>Middle Name</label></td>
  11. <td><input type="text" name="middle_name" placeholder="Middle Name....." autofocus="autofocus"></td>
  12. </tr>
  13. <tr>
  14. <td><label>Last Name</label></td>
  15. <td><input type="text" name="last_name" placeholder="Last Name....." autofocus="autofocus"></td>
  16. </tr>
  17. <tr>
  18. <td><label>Email</label></td>
  19. <td><input type="email" name="email" placeholder="Email....." autofocus="autofocus"></td>
  20. </tr>
  21. <tr>
  22. <td><label>Contact Number</label></td>
  23. <td><input type="text" placeholder="Contact Number....." maxlength="13" name="contact_number" autofocus="autofocus"></td>
  24. </tr>
  25. <tr>
  26. <td colspan="2">
  27. <button class="submit_btn" type="submit" name="submit">Add</button>
  28. <button class="cancel_btn" type="reset" name="cancel">Cancel</button>
  29. </td>
  30. </tr>
  31.  
  32. </form>

Creating Database Connection Using PDO

  1. <?php
  2. $conn = new PDO("mysql:host=localhost;dbname=add_query_pdo", 'root', '');
  3. ?>

Creating Insert Into Statement

This PHP query used to insert data into a database table.

  1. <?php
  2. include ('connection.php');
  3.  
  4. $first_name=$_POST['first_name'];
  5. $middle_name=$_POST['middle_name'];
  6. $last_name=$_POST['last_name'];
  7. $email=$_POST['email'];
  8. $contact_number=$_POST['contact_number'];
  9.  
  10. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  11. $sql = "INSERT INTO tbl_registration (first_name, middle_name, last_name, email, contact_number)
  12. VALUES ('$first_name', '$middle_name', '$last_name', '$email', '$contact_number')";
  13.  
  14. $conn->exec($sql);
  15. echo "<script>alert('Successfully Added!'); window.location='index.php'</script>";
  16. ?>

And, this is the style.

  1. <style type="text/css">
  2. body {
  3. width:600px;
  4. border:blue 1px solid;
  5. border-style:dashed;
  6. margin:auto;
  7. padding:10px;
  8. margin-top: 40px;
  9. }
  10. td {
  11. text-align:center;
  12. padding:10px;
  13. }
  14. table {
  15. margin:auto;
  16. }
  17. input[type="text"] {
  18. width:200px;
  19. font-size: 18px;
  20. border: blue 1px solid;
  21. text-indent: 5px;
  22. cursor:pointer;
  23. }
  24. input[type="email"] {
  25. width:200px;
  26. font-size: 18px;
  27. border: blue 1px solid;
  28. text-indent: 5px;
  29. cursor:pointer;
  30. }
  31. label {
  32. font-size:18px;
  33. color:red;
  34. font-weight: bold;
  35. font-family: cursive;
  36. }
  37. h2 {
  38. color:blue;
  39. text-align:center;
  40. }
  41. .submit_btn {
  42. margin-right: 70px;
  43. border: blue 1px solid;
  44. font-size: 18px;
  45. font-family: cursive;
  46. padding: 5px;
  47. font-weight: bold;
  48. color: blue;
  49. background: azure;
  50. border-radius: 4px;
  51. }
  52. .submit_btn:hover {
  53. margin-right: 70px;
  54. border: red 1px solid;
  55. font-size: 18px;
  56. font-family: cursive;
  57. padding: 5px;
  58. font-weight: bold;
  59. color: white;
  60. background: blue;
  61. border-radius: 4px;
  62. cursor:pointer;
  63. }
  64. .cancel_btn {
  65. border: red 1px solid;
  66. font-size: 18px;
  67. font-family: cursive;
  68. padding: 5px;
  69. font-weight: bold;
  70. color: red;
  71. background: azure;
  72. border-radius: 4px;
  73. }
  74. .cancel_btn:hover {
  75. border: blue 1px solid;
  76. font-size: 18px;
  77. font-family: cursive;
  78. padding: 5px;
  79. font-weight: bold;
  80. color: white;
  81. background: red;
  82. border-radius: 4px;
  83. cursor:pointer;
  84. }
  85. </style>

Output:

Where the information type in the form field.

1stAfter clicking the "Add" button. This is the data in the database table.

Result

Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.