Skip to main content

Insert Contact Information in PHP/MySQL Using PDO

Insert Contact Information

If you are looking for Insert Contact Information tutorial then you are at the right place. In this article, we are going to learn about on how to add contact information in the database and you can see all your contact on your page.

Let's start with:

Creating our Table

We are going to make our database.

  1. Open the PHPMyAdmin.
  2. Create a database and name it as "contact_info".
  3. After creating a database name, then we are going to create our table. And name it as "tbl_member".
  4. Kindly copy the code below.

  1. CREATE TABLE `tbl_member` (
  2. `tbl_member_id` INT(11) NOT NULL,
  3. `first_name` VARCHAR(100) NOT NULL,
  4. `last_name` VARCHAR(100) NOT NULL,
  5. `contact_number` VARCHAR(100) NOT NULL,
  6. `email` VARCHAR(100) NOT NULL,
  7. `address` VARCHAR(100) NOT NULL

Database Connection

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

Add Form Field

  1. <form method="post" action="add_person_query.php">
  2. <table border="1" cellspacing="5" cellpadding="5" width="100%">
  3. <tr>
  4. <td>
  5. <label>First Name</label>
  6. </td>
  7. <td width="395px">
  8. <input type="text" class="text_Box" name="first_name" autofocus="autofocus" placeholder="First Name ....." required />
  9. </td>
  10. </tr>
  11. <tr>
  12. <td>
  13. <label>Last Name</label>
  14. </td>
  15. <td width="395px">
  16. <input type="text" class="text_Box" name="last_name" placeholder="Last Name ....." required />
  17. </td>
  18. </tr>
  19. <tr>
  20. <td>
  21. <label>Contact Number</label>
  22. </td>
  23. <td width="395px">
  24. <input type="text" class="text_Box" name="contact_number" placeholder="Contact Number ....." required />
  25. </td>
  26. </tr>
  27. <tr>
  28. <td>
  29. <label>Email</label>
  30. </td>
  31. <td width="395px">
  32. <input type="email" class="text_Box" name="email" placeholder="Email .....">
  33. </td>
  34. </tr>
  35. <tr>
  36. <td>
  37. <label>Address</label>
  38. </td>
  39. <td width="395px">
  40. <input type="text" class="text_Box" name="address" placeholder="Address ....." required />
  41. </td>
  42. </tr>
  43. <tr>
  44. <td colspan="2">
  45. <a style="margin-right:70px;">
  46. <button type="submit" class="btn_confirm">
  47. Save Data
  48. </button>
  49. </a>
  50. <a href="index.php">
  51. <button type="submit" class="btn_cancel">
  52. Cancel
  53. </button>
  54. </a>
  55. </td>
  56. </tr>
  57. </form>

Add - PHP Query Using PDO

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

Viewing Data

  1. <table border="1" cellspacing="5" cellpadding="5" width="100%">
  2. <tr>
  3. <th>First Name</th>
  4. <th>Last Name</th>
  5. <th>Contact Number</th>
  6. <th>Email</th>
  7. <th>Address</th>
  8. </tr>
  9. </thead>
  10. <?php
  11. require_once('db.php');
  12. $result = $conn->prepare("SELECT * FROM tbl_member ORDER BY tbl_member_id ASC");
  13. $result->execute();
  14. for($i=0; $row = $result->fetch(); $i++){
  15. $id=$row['tbl_member_id'];
  16. ?>
  17. <tr>
  18. <td><label><?php echo $row['first_name']; ?></label></td>
  19. <td><label><?php echo $row['last_name']; ?></label></td>
  20. <td><label><?php echo $row['contact_number']; ?></label></td>
  21. <td><label><?php echo $row['email']; ?></label></td>
  22. <td><label><?php echo $row['address']; ?></label></td>
  23. </tr>
  24. <?php } ?>
  25. </tbody>


Adding new Data.


Viewing Data.


And, that's it. This is the steps on how to insert contact in the database. For my next tutorial, we are going to create on how to Update Contact Information.

Kindly click the "Download Code" button for full source code. Thank very much.

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.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.