Update Contact Information in PHP/MySQL Using PDO

Related Code: Insert Contact Information In the previous tutorial, Insert Contact Information, we learn on how to insert contact information in the database. For the follow-up tutorial, we are going to create on how to Update Contact Information in the database.

Edit - Form Field

  1. <?php
  2. include('db.php');
  3. $result = $conn->prepare("SELECT * FROM tbl_member where tbl_member_id='$ID'");
  4. $result->execute();
  5. for($i=0; $row = $result->fetch(); $i++){
  6. $id=$row['tbl_member_id'];
  7. ?>
  8.  
  9. <form method="post" action="edit_person_query.php<?php echo '?tbl_member_id='.$id; ?>">
  10. <table border="1" cellspacing="5" cellpadding="5" width="100%">
  11. <tr>
  12. <td>
  13. <label>First Name</label>
  14. </td>
  15. <td width="395px">
  16. <input type="text" class="text_Box" name="first_name" value="<?php echo $row['first_name']; ?>" autofocus="autofocus" placeholder="First Name ....." />
  17. </td>
  18. </tr>
  19. <tr>
  20. <td>
  21. <label>Last Name</label>
  22. </td>
  23. <td width="395px">
  24. <input type="text" class="text_Box" name="last_name" value="<?php echo $row['last_name']; ?>" placeholder="Last Name ....." />
  25. </td>
  26. </tr>
  27. <tr>
  28. <td>
  29. <label>Contact Number</label>
  30. </td>
  31. <td width="395px">
  32. <input type="text" class="text_Box" name="contact_number" value="<?php echo $row['contact_number']; ?>" placeholder="Contact Number ....." />
  33. </td>
  34. </tr>
  35. <tr>
  36. <td>
  37. <label>Email</label>
  38. </td>
  39. <td width="395px">
  40. <input type="email" class="text_Box" name="email" value="<?php echo $row['email']; ?>" placeholder="Email .....">
  41. </td>
  42. </tr>
  43. <tr>
  44. <td>
  45. <label>Address</label>
  46. </td>
  47. <td width="395px">
  48. <input type="text" class="text_Box" name="address" value="<?php echo $row['address']; ?>" placeholder="Address ....." />
  49. </td>
  50. </tr>
  51. <tr>
  52. <td colspan="2">
  53. <a>
  54. <button type="submit" class="btn_confirm">
  55. Save Data
  56. </button>
  57. </a>
  58. </td>
  59. </tr>
  60. </form>
  61. <?php } ?>

Edit - PHP Query Using PDO

  1. <?php
  2. include 'db.php';
  3.  
  4. $get_id=$_REQUEST['tbl_member_id'];
  5.  
  6. $first_name= $_POST['first_name'];
  7. $last_name= $_POST['last_name'];
  8. $contact_number= $_POST['contact_number'];
  9. $email= $_POST['email'];
  10. $address= $_POST['address'];
  11.  
  12. $sql = "UPDATE tbl_member SET first_name ='$first_name', last_name ='$last_name',
  13. contact_number ='$contact_number', email ='$email', address ='$address' WHERE tbl_member_id = '$get_id' ";
  14.  
  15. $conn->exec($sql);
  16. echo "<script>alert('Successfully Edit The Account!'); window.location='index.php'</script>";
  17. ?>

Result:

We are going to update the data. ResultAfter updating the data. Result And, that's it. This is the steps on how to update a contact in the database. For my next tutorial, we are going to create on how to Delete 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.

Add new comment