OOP PHP CRUD Operation Using MySQLi - Part 2
Submitted by razormist on Friday, January 20, 2017 - 16:47.
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.
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.
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'
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'
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
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!!
- <?php
- require 'config.php';
- class db_class extends db_connect{
- public function __construct(){
- $this->connect();
- }
- public function create($firstname, $lastname){
- $stmt = $this->conn->prepare("INSERT INTO `member` (`firstname`, `lastname`) VALUES (?, ?)") or die($this->conn->error);
- $stmt->bind_param("ss", $firstname, $lastname);
- if($stmt->execute()){
- $stmt->close();
- $this->conn->close();
- return true;
- }
- }
- public function read(){
- $stmt = $this->conn->prepare("SELECT * FROM `member` ORDER BY `lastname` ASC") or die($this->conn->error);
- if($stmt->execute()){
- $result = $stmt->get_result();
- $stmt->close();
- $this->conn->close();
- return $result;
- }
- }
- public function member_id($mem_id){
- $stmt = $this->conn->prepare("SELECT * FROM `member` WHERE `mem_id` = '$mem_id'") or die($this->conn->error);
- if($stmt->execute()){
- $result = $stmt->get_result();
- $fetch = $result->fetch_array();
- $stmt->close();
- $this->conn->close();
- return $fetch;
- }
- }
- public function delete($mem_id){
- $stmt = $this->conn->prepare("DELETE FROM `member` WHERE `mem_id` = '$mem_id'") or die($this->conn->error);
- if($stmt->execute()){
- $stmt->close();
- $this->conn->close();
- return true;
- }
- }
- public function update($firstname, $lastname, $mem_id){
- $stmt = $this->conn->prepare("UPDATE `member` SET `firstname` = '$firstname', `lastname` = '$lastname' WHERE `mem_id` = '$mem_id'") or die($this->conn->error);
- if($stmt->execute()){
- $stmt->close();
- $this->conn->close();
- return true;
- }
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang = "en">
- <head>
- <meta charset = "UTF-8" name = "viewport" content = "width-device=width, initial-scale=1" />
- <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css" />
- <title>OOP PHP CRUD Operation</title>
- </head>
- <body>
- <nav class = "navbar navbar-default">
- <div class = "container-fluid">
- <a class = "navbar-brand" href = "https://www.sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class = "row">
- <div class = "col-md-3">
- </div>
- <div class = "col-md-6 well">
- <h3 class = "text-primary">OOP PHP CRUD Operation Using MySQLi - Part 2</h3>
- <hr style = "border-top:1px dotted #000;"/>
- <form method = "POST" class = "form-inline" action = "create.php">
- <div class = "form-group">
- <label>Firstname:</label>
- <input type = "text" id = "firstname" name = "firstname" class = "form-control" required = "required"/>
- </div>
- <div class = "form-group">
- <label>Lastname:</label>
- <input type = "text" id = "lastname" name = "lastname" class = "form-control" required = "required"/>
- </div>
- <div class = "form-group">
- <button name = "save" class = "btn btn-primary"><span class = "glyphicon glyphicon-plus"></span> Add</button>
- </div>
- </form>
- <br />
- <table class = "table table-bordered alert-warning table-hover">
- <thead>
- <th>Firstname</th>
- <th>Lastname</th>
- <th>Action</th>
- </thead>
- <tbody>
- <?php
- require 'class.php';
- $conn = new db_class();
- $read = $conn->read();
- while($fetch = $read->fetch_array()){
- ?>
- <tr>
- <td><?php echo $fetch['firstname']?></td>
- <td><?php echo $fetch['lastname']?></td>
- <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>
- </tr>
- <?php
- }
- ?>
- </tbody>
- </table>
- </div>
- </div>
- <!-- Modal -->
- <div class="modal fade" id="del_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
- <div class="modal-dialog" role="document">
- <div class="modal-content">
- <div class="modal-body">
- <center><h4 class = "text-danger">Are you sure you want to delete this record?</h4></center>
- </div>
- <div class="modal-footer">
- <button type = "button" class="btn btn-warning" data-dismiss="modal"><span class = "glyphicon glyphicon-remove"></span> No</button>
- <button type = "button" class="btn btn-danger del_mem"><span class = "glyphicon glyphicon-trash"></span> Yes</button>
- </div>
- </div>
- </div>
- </div>
- <div class="modal fade" id="update_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
- <div class="modal-dialog" role="document">
- <div class="modal-content">
- <div class = "modal-header">
- <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
- <h3 class = "text-success modal-title">Update Member</h3>
- </div>
- <form method = "POST" action = "update_mem_query.php">
- <div class="modal-body update">
- </div>
- <div class="modal-footer">
- <button class="btn btn-warning" name = "update"><span class = "glyphicon glyphicon-edit"></span> Save Changes</button>
- </div>
- </form>
- </div>
- </div>
- </div>
- </body>
- <script src = "js/jquery-3.1.1.js"></script>
- <script src = "js/bootstrap.js"></script>
- <script type = "text/javascript">
- $(document).ready(function(){
- //Delete
- $('.del_mem_id').on('click', function(){
- $mem_id = $(this).attr('name');
- $('.del_mem').on('click', function(){
- window.location = "delete_mem.php?mem_id=" + $mem_id;
- });
- });
- //Update
- $('.update_mem_id').on('click', function(){
- $mem_id = $(this).attr('name');
- $('.update').load('mem_data.php?mem_id=' + $mem_id);
- });
- });
- </script>
- </html>
- <?php
- require_once 'class.php';
- $mem_id = $_REQUEST['mem_id'];
- $conn = new db_class();
- $conn->delete($mem_id);
- ?>
- <?php
- $mem_id = $_REQUEST['mem_id'];
- require_once 'class.php';
- $conn = new db_class();
- $fetch = $conn->member_id($mem_id);
- ?>
- <div class = "form-group">
- <label>Firstname</label>
- <input type = "text" name = "firstname" value = "<?php echo $fetch['firstname']?>" class = "form-control" />
- <input type = "hidden" name = "mem_id" value = "<?php echo $mem_id?>" />
- </div>
- <div class = "form-group">
- <label>Lastname</label>
- <input type = "text" name = "lastname" value = "<?php echo $fetch['lastname']?>" class = "form-control" />
- </div>
- <?php
- require_once 'class.php';
- $firstname = $_POST['firstname'];
- $lastname = $_POST['lastname'];
- $mem_id = $_POST['mem_id'];
- $conn = new db_class();
- $conn->update($firstname, $lastname, $mem_id);
- echo '
- <script>alert("Updated Successfully")</script>;
- <script>window.location = "index.php"</script>;
- ';
- }
- ?>
Comments
Add new comment
- Add new comment
- 379 views