Skip to main content

OOP PHP CRUD Operation Using MySQLi - Part 2

Screenshot
Body
In this tutorial, we will deal with OOP PHP CRUD functions, in my previous tutorial we created a simple application that can create and read data from the database and display it on the HTML form at the same time. Now this time we will continue on our previous project, and we will be adding a Delete and Update function. Before we continue I hope that you read OOP PHP CRUD Operation Using MySQLi - Part 1 so that you have an idea how this tutorial will be going. So let's get started its coding time! *If you already have the previous source code just open it, and we will still be working with it The CRUD We already created a database and a database connection from our previous tutorial, so we will just add some new code, just copy/paste the whole code below or you can just copy/paste the new one.
  1. <?php
  2. require 'config.php';
  3.  
  4. class db_class extends db_connect{
  5.  
  6. public function __construct(){
  7. $this->connect();
  8. }
  9.  
  10. public function create($firstname, $lastname){
  11. $stmt = $this->conn->prepare("INSERT INTO `member` (`firstname`, `lastname`) VALUES (?, ?)") or die($this->conn->error);
  12. $stmt->bind_param("ss", $firstname, $lastname);
  13. if($stmt->execute()){
  14. $stmt->close();
  15. $this->conn->close();
  16. return true;
  17. }
  18. }
  19.  
  20. public function read(){
  21. $stmt = $this->conn->prepare("SELECT * FROM `member` ORDER BY `lastname` ASC") or die($this->conn->error);
  22. if($stmt->execute()){
  23. $result = $stmt->get_result();
  24. $stmt->close();
  25. $this->conn->close();
  26. return $result;
  27. }
  28. }
  29.  
  30. public function member_id($mem_id){
  31. $stmt = $this->conn->prepare("SELECT * FROM `member` WHERE `mem_id` = '$mem_id'") or die($this->conn->error);
  32. if($stmt->execute()){
  33. $result = $stmt->get_result();
  34. $fetch = $result->fetch_array();
  35. $stmt->close();
  36. $this->conn->close();
  37. return $fetch;
  38. }
  39. }
  40.  
  41. public function delete($mem_id){
  42. $stmt = $this->conn->prepare("DELETE FROM `member` WHERE `mem_id` = '$mem_id'") or die($this->conn->error);
  43. if($stmt->execute()){
  44. $stmt->close();
  45. $this->conn->close();
  46. return true;
  47. }
  48. }
  49.  
  50. public function update($firstname, $lastname, $mem_id){
  51. $stmt = $this->conn->prepare("UPDATE `member` SET `firstname` = '$firstname', `lastname` = '$lastname' WHERE `mem_id` = '$mem_id'") or die($this->conn->error);
  52. if($stmt->execute()){
  53. $stmt->close();
  54. $this->conn->close();
  55. return true;
  56. }
  57. }
  58. }
  59. ?>
The code above will generate the request after the function is called. The following function that I have added are member_id(), delete() and update(). The member_id() function will get the member_id from the targeted button,and then display the exact value when the update button has been clicked, where in the user can change the value of each form. The delete() function will delete the targeted button. And the update() function will update the data within the form. The Mark-up Form This is the form we will use in the CRUD functions, copy/paste the code below, I have added a bootstrap modal here, along with jQuery script.
  1. <!DOCTYPE html>
  2. <html lang = "en">
  3. <head>
  4. <meta charset = "UTF-8" name = "viewport" content = "width-device=width, initial-scale=1" />
  5. <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css" />
  6. <title>OOP PHP CRUD Operation</title>
  7. </head>
  8. <body>
  9. <nav class = "navbar navbar-default">
  10. <div class = "container-fluid">
  11. <a class = "navbar-brand" href = "https://www.sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </nav>
  14. <div class = "row">
  15. <div class = "col-md-3">
  16. </div>
  17. <div class = "col-md-6 well">
  18. <h3 class = "text-primary">OOP PHP CRUD Operation Using MySQLi - Part 2</h3>
  19. <hr style = "border-top:1px dotted #000;"/>
  20. <form method = "POST" class = "form-inline" action = "create.php">
  21. <div class = "form-group">
  22. <label>Firstname:</label>
  23. <input type = "text" id = "firstname" name = "firstname" class = "form-control" required = "required"/>
  24. </div>
  25. <div class = "form-group">
  26. <label>Lastname:</label>
  27. <input type = "text" id = "lastname" name = "lastname" class = "form-control" required = "required"/>
  28. </div>
  29. <div class = "form-group">
  30. <button name = "save" class = "btn btn-primary"><span class = "glyphicon glyphicon-plus"></span> Add</button>
  31. </div>
  32. </form>
  33. <br />
  34. <table class = "table table-bordered alert-warning table-hover">
  35. <thead>
  36. <th>Firstname</th>
  37. <th>Lastname</th>
  38. <th>Action</th>
  39. </thead>
  40. <tbody>
  41. <?php
  42. require 'class.php';
  43. $conn = new db_class();
  44. $read = $conn->read();
  45. while($fetch = $read->fetch_array()){
  46. ?>
  47. <tr>
  48. <td><?php echo $fetch['firstname']?></td>
  49. <td><?php echo $fetch['lastname']?></td>
  50. <td><center><a class = "btn btn-warning update_mem_id" data-toggle = "modal" data-target = "#update_modal" name = "<?php echo $fetch['mem_id']?>"><span class = "glyphicon glyphicon-edit"></span> Update</a> | <a class = "btn btn-danger del_mem_id" name = "<?php echo $fetch['mem_id']?>" data-toggle = "modal" data-target="#del_modal"><span class = "glyphicon glyphicon-trash"></span> Delete</a></center></td>
  51. </tr>
  52. <?php
  53. }
  54. ?>
  55. </tbody>
  56. </table>
  57. </div>
  58. </div>
  59.  
  60. <!-- Modal -->
  61. <div class="modal fade" id="del_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  62. <div class="modal-dialog" role="document">
  63. <div class="modal-content">
  64. <div class="modal-body">
  65. <center><h4 class = "text-danger">Are you sure you want to delete this record?</h4></center>
  66. </div>
  67. <div class="modal-footer">
  68. <button type = "button" class="btn btn-warning" data-dismiss="modal"><span class = "glyphicon glyphicon-remove"></span> No</button>
  69. <button type = "button" class="btn btn-danger del_mem"><span class = "glyphicon glyphicon-trash"></span> Yes</button>
  70. </div>
  71. </div>
  72. </div>
  73. </div>
  74. <div class="modal fade" id="update_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  75. <div class="modal-dialog" role="document">
  76. <div class="modal-content">
  77. <div class = "modal-header">
  78. <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  79. <h3 class = "text-success modal-title">Update Member</h3>
  80. </div>
  81. <form method = "POST" action = "update_mem_query.php">
  82. <div class="modal-body update">
  83.  
  84. </div>
  85. <div class="modal-footer">
  86. <button class="btn btn-warning" name = "update"><span class = "glyphicon glyphicon-edit"></span> Save Changes</button>
  87. </div>
  88. </form>
  89. </div>
  90. </div>
  91. </div>
  92. </body>
  93. <script src = "js/jquery-3.1.1.js"></script>
  94. <script src = "js/bootstrap.js"></script>
  95. <script type = "text/javascript">
  96. $(document).ready(function(){
  97.  
  98. //Delete
  99. $('.del_mem_id').on('click', function(){
  100. $mem_id = $(this).attr('name');
  101. $('.del_mem').on('click', function(){
  102. window.location = "delete_mem.php?mem_id=" + $mem_id;
  103. });
  104. });
  105.  
  106. //Update
  107. $('.update_mem_id').on('click', function(){
  108. $mem_id = $(this).attr('name');
  109. $('.update').load('mem_data.php?mem_id=' + $mem_id);
  110. });
  111. });
  112. </script>
  113. </html>
Below the closing body tag I added a jQuery script here to make the modal works with PHP functions. The Delete query We will now create the delete query, copy/paste the code below, and name it 'delete_mem.php'
  1. <?php
  2. require_once 'class.php';
  3.  
  4. $mem_id = $_REQUEST['mem_id'];
  5. $conn = new db_class();
  6. $conn->delete($mem_id);
  7. header('location:index.php');
  8. ?>
The code above will call the delete() function, and then it will delete the the targeted value base on the member_id. The Member data for updating Creating the member data, copy/paste the code below, then name it 'mem_data.php'
  1. <?php
  2. $mem_id = $_REQUEST['mem_id'];
  3. require_once 'class.php';
  4. $conn = new db_class();
  5. $fetch = $conn->member_id($mem_id);
  6. ?>
  7. <div class = "form-group">
  8. <label>Firstname</label>
  9. <input type = "text" name = "firstname" value = "<?php echo $fetch['firstname']?>" class = "form-control" />
  10. <input type = "hidden" name = "mem_id" value = "<?php echo $mem_id?>" />
  11. </div>
  12. <div class = "form-group">
  13. <label>Lastname</label>
  14. <input type = "text" name = "lastname" value = "<?php echo $fetch['lastname']?>" class = "form-control" />
  15. </div>
The code above will display the data that you want to update, after the update button has been clicked, and will be extracted into the modal-body with the used of jQuery script. The Update query
  1. <?php
  2. require_once 'class.php';
  3. if(ISSET($_POST['update'])){
  4. $firstname = $_POST['firstname'];
  5. $lastname = $_POST['lastname'];
  6. $mem_id = $_POST['mem_id'];
  7. $conn = new db_class();
  8. $conn->update($firstname, $lastname, $mem_id);
  9. echo '
  10. <script>alert("Updated Successfully")</script>;
  11. <script>window.location = "index.php"</script>;
  12. ';
  13. }
  14. ?>
The code above stored all the value within the form and call the function update() to put all the updated data into the database. There you have it we created the OOP PHP CRUD function. I hope that this tutorial help you understand on how to deal with Object Oriented Programming. For more updates and tutorials just visit this site. Enjoy Coding!!

Tags

Comments

Submitted byZaphkiel17 (not verified)on Fri, 02/07/2020 - 15:10

Hi im new in programming How to print the error message in a function i want to display "connection success" and "connection failed" i put echo but nothing happens

Add new comment