PHP CRUD using OOP Approach

Getting Started

To add a little design to our application, I've used CDN for Bootstrap and jQuery in this tutorial so you need internet connection for them to work.

Creating our Database

First, we need to create our MySQL database. 1. Open phpMyAdmin. 2. Click databases, create a database and name it as crud. 3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.
  1. CREATE TABLE `members` (
  2. `firstname` varchar(30) NOT NULL,
  3. `lastname` varchar(30) NOT NULL,
  4. `address` text NOT NULL,
database mysql

Creating our Connection

Next, we are going to create our connection to our database using the OOP approach wherein we are going to create a class for the connection. We are going to name this file as DbConnection.php.
  1. <?php
  2. class DbConnection
  3. {
  4. private $host = 'localhost';
  5. private $username = 'root';
  6. private $password = '';
  7. private $database = 'crud';
  8.  
  9. protected $connection;
  10.  
  11. public function __construct(){
  12.  
  13. if (!isset($this->connection)) {
  14.  
  15. $this->connection = new mysqli($this->host, $this->username, $this->password, $this->database);
  16.  
  17. if (!$this->connection) {
  18. echo 'Cannot connect to database server';
  19. }
  20. }
  21.  
  22. return $this->connection;
  23. }
  24. }
  25. ?>

Creating our Database Action

Next, we create our action to our database. This is where we perform our CRUD Operations and of course using OOP approach as well. We're gonna name this file as Crud.php.
  1. <?php
  2. include_once('DbConnection.php');
  3.  
  4. class Crud extends DbConnection
  5. {
  6. public function __construct(){
  7.  
  8. parent::__construct();
  9. }
  10.  
  11. public function read($sql){
  12.  
  13. $query = $this->connection->query($sql);
  14.  
  15. if ($query == false) {
  16. return false;
  17. }
  18.  
  19. $rows = array();
  20.  
  21. while ($row = $query->fetch_array()) {
  22. $rows[] = $row;
  23. }
  24.  
  25. return $rows;
  26. }
  27.  
  28. public function execute($sql){
  29.  
  30. $query = $this->connection->query($sql);
  31.  
  32. if ($query == false) {
  33. return false;
  34. } else {
  35. return true;
  36. }
  37. }
  38.  
  39. public function escape_string($value){
  40.  
  41. return $this->connection->real_escape_string($value);
  42. }
  43. }

index.php

This is our index where we fetch table data from our MySQL database.
  1. <?php
  2. //start session
  3.  
  4. //crud with database connection
  5. include_once('Crud.php');
  6.  
  7. $crud = new Crud();
  8.  
  9. //fetch data
  10. $sql = "SELECT * FROM members";
  11. $result = $crud->read($sql);
  12. ?>
  13. <!DOCTYPE html>
  14. <html>
  15. <head>
  16. <meta charset="utf-8">
  17. <title>PHP CRUD using OOP Approach</title>
  18. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  19. </head>
  20. <body>
  21. <div class="container">
  22. <h1 class="page-header text-center">PHP CRUD using OOP Approach</h1>
  23. <div class="row">
  24. <div class="col-sm-8 col-sm-offset-2">
  25. <?php
  26. if(isset($_SESSION['message'])){
  27. ?>
  28. <div class="alert alert-info text-center">
  29. <?php echo $_SESSION['message']; ?>
  30. </div>
  31. <?php
  32.  
  33. unset($_SESSION['message']);
  34. }
  35.  
  36. ?>
  37. <a href="#add" data-toggle="modal" class="btn btn-primary">Add New</a><br><br>
  38. <table class="table table-bordered table-striped">
  39. <thead>
  40. <tr>
  41. <th>ID</th>
  42. <th>Firstname</th>
  43. <th>Lastname</th>
  44. <th>Address</th>
  45. <th>Action</th>
  46. </tr>
  47. </thead>
  48. <tbody>
  49. <?php
  50. foreach ($result as $key => $row) {
  51. ?>
  52. <tr>
  53. <td><?php echo $row['id']; ?></td>
  54. <td><?php echo $row['firstname']; ?></td>
  55. <td><?php echo $row['lastname']; ?></td>
  56. <td><?php echo $row['address']; ?></td>
  57. <td><a href="#edit<?php echo $row['id']; ?>" data-toggle="modal" class="btn btn-success">Edit</a> |
  58. <a href="#delete<?php echo $row['id']; ?>" data-toggle="modal" class="btn btn-danger">Delete</a>
  59. </td>
  60. <?php include('action_modal.php'); ?>
  61. </tr>
  62. <?php
  63. }
  64. ?>
  65. </tbody>
  66. </table>
  67. </div>
  68. </div>
  69. </div>
  70. <?php include('add_modal.php'); ?>
  71. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  72. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" rel="stylesheet"></script>
  73. </body>
  74. </html>

add_modal.php

This is our included modal which contains our add form.
  1. <!-- Add New -->
  2. <div class="modal fade" id="add" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  3. <div class="modal-dialog">
  4. <div class="modal-content">
  5. <div class="modal-header">
  6. <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
  7. <center><h4 class="modal-title" id="myModalLabel">Add Member</h4></center>
  8. </div>
  9. <div class="modal-body">
  10. <div class="container-fluid">
  11. <form method="POST" action="add.php">
  12. <div class="row">
  13. <div class="col-lg-2">
  14. <label class="control-label" style="position:relative; top:7px;">Firstname:</label>
  15. </div>
  16. <div class="col-lg-10">
  17. <input type="text" class="form-control" name="firstname">
  18. </div>
  19. </div>
  20. <div style="height:10px;"></div>
  21. <div class="row">
  22. <div class="col-lg-2">
  23. <label class="control-label" style="position:relative; top:7px;">Lastname:</label>
  24. </div>
  25. <div class="col-lg-10">
  26. <input type="text" class="form-control" name="lastname">
  27. </div>
  28. </div>
  29. <div style="height:10px;"></div>
  30. <div class="row">
  31. <div class="col-lg-2">
  32. <label class="control-label" style="position:relative; top:7px;">Address:</label>
  33. </div>
  34. <div class="col-lg-10">
  35. <input type="text" class="form-control" name="address">
  36. </div>
  37. </div>
  38. </div>
  39. </div>
  40. <div class="modal-footer">
  41. <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
  42. <button type="submit" name="add" class="btn btn-primary">Save</button>
  43. </form>
  44. </div>
  45.  
  46. </div>
  47. </div>
  48. </div>

action_modal.php

This is also our included modal that contains our edit and delete modal.
  1. <!-- Delete -->
  2. <div class="modal fade" id="delete<?php echo $row['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  3. <div class="modal-dialog">
  4. <div class="modal-content">
  5. <div class="modal-header">
  6. <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
  7. <center><h4 class="modal-title" id="myModalLabel">Delete Member</h4></center>
  8. </div>
  9. <div class="modal-body">
  10. <div class="container-fluid text-center">
  11. <h5>Are sure you want to delete</h5>
  12. <h2>Name: <b><?php echo $row['firstname'].' '.$row['lastname']; ?></b></h2>
  13. </div>
  14. </div>
  15. <div class="modal-footer">
  16. <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
  17. <a href="delete.php?id=<?php echo $row['id']; ?>" class="btn btn-danger">Yes</a>
  18. </div>
  19. </div>
  20. </div>
  21. </div>
  22. <!-- /.modal -->
  23.  
  24. <!-- Edit -->
  25. <div class="modal fade" id="edit<?php echo $row['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  26. <div class="modal-dialog">
  27. <div class="modal-content">
  28. <div class="modal-header">
  29. <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
  30. <center><h4 class="modal-title" id="myModalLabel">Edit Member</h4></center>
  31. </div>
  32. <div class="modal-body">
  33. <div class="container-fluid">
  34. <form method="POST" action="edit.php?id=<?php echo $row['id']; ?>">
  35. <div class="row">
  36. <div class="col-lg-2">
  37. <label style="position:relative; top:7px;">Firstname:</label>
  38. </div>
  39. <div class="col-lg-10">
  40. <input type="text" name="firstname" class="form-control" value="<?php echo $row['firstname']; ?>">
  41. </div>
  42. </div>
  43. <div style="height:10px;"></div>
  44. <div class="row">
  45. <div class="col-lg-2">
  46. <label style="position:relative; top:7px;">Lastname:</label>
  47. </div>
  48. <div class="col-lg-10">
  49. <input type="text" name="lastname" class="form-control" value="<?php echo $row['lastname']; ?>">
  50. </div>
  51. </div>
  52. <div style="height:10px;"></div>
  53. <div class="row">
  54. <div class="col-lg-2">
  55. <label style="position:relative; top:7px;">Address:</label>
  56. </div>
  57. <div class="col-lg-10">
  58. <input type="text" name="address" class="form-control" value="<?php echo $row['address']; ?>">
  59. </div>
  60. </div>
  61. </div>
  62. </div>
  63. <div class="modal-footer">
  64. <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
  65. <button type="submit" name="edit" class="btn btn-warning">Save</button>
  66. </div>
  67. </form>
  68. </div>
  69. </div>
  70. </div>
  71. <!-- /.modal -->

add.php

This is our PHP code in adding data to our database.
  1. <?php
  2. //start session
  3.  
  4. //including the database connection file
  5. include_once('Crud.php');
  6.  
  7. $crud = new Crud();
  8.  
  9. if(isset($_POST['add'])) {
  10. $firstname = $crud->escape_string($_POST['firstname']);
  11. $lastname = $crud->escape_string($_POST['lastname']);
  12. $address = $crud->escape_string($_POST['address']);
  13.  
  14. //insert data to database
  15. $sql = "INSERT INTO members (firstname, lastname, address) VALUES ('$firstname','$lastname','$address')";
  16.  
  17. if($crud->execute($sql)){
  18. $_SESSION['message'] = 'Member added successfully';
  19. }
  20. else{
  21. $_SESSION['message'] = 'Cannot add member';
  22. }
  23.  
  24. header('location: index.php');
  25. }
  26. else{
  27. $_SESSION['message'] = 'Fill up add form first';
  28. header('location: index.php');
  29. }
  30. ?>

edit.php

This is our PHP code that updates data in our database.
  1. <?php
  2. //start session
  3.  
  4. //including the database connection file
  5. include_once('Crud.php');
  6.  
  7. //getting the id
  8. $id = $_GET['id'];
  9.  
  10. $crud = new Crud();
  11.  
  12. if(isset($_POST['edit'])) {
  13. $firstname = $crud->escape_string($_POST['firstname']);
  14. $lastname = $crud->escape_string($_POST['lastname']);
  15. $address = $crud->escape_string($_POST['address']);
  16.  
  17. //update data
  18. $sql = "UPDATE members SET firstname = '$firstname', lastname = '$lastname', address = '$address' WHERE id = '$id'";
  19.  
  20. if($crud->execute($sql)){
  21. $_SESSION['message'] = 'Member updated successfully';
  22. }
  23. else{
  24. $_SESSION['message'] = 'Cannot update member';
  25. }
  26.  
  27. header('location: index.php');
  28. }
  29. else{
  30. $_SESSION['message'] = 'Select user to edit first';
  31. header('location: index.php');
  32. }
  33. ?>

delete.php

Lastly, this is our PHP code that deletes the data in our database.
  1. <?php
  2. //start session
  3.  
  4. //including the database connection file
  5. include_once('Crud.php');
  6.  
  7. if(isset($_GET['id'])){
  8.  
  9. //getting the id
  10. $id = $_GET['id'];
  11.  
  12. $crud = new Crud();
  13.  
  14. //delete data
  15. $sql = "DELETE FROM members WHERE id = '$id'";
  16.  
  17. if($crud->execute($sql)){
  18. $_SESSION['message'] = 'Member deleted successfully';
  19. }
  20. else{
  21. $_SESSION['message'] = 'Cannot delete member';
  22. }
  23.  
  24. header('location: index.php');
  25. }
  26. else{
  27. $_SESSION['message'] = 'Select user to delete first';
  28. header('location: index.php');
  29. }
  30. ?>
That ends this tutorial. Happy Coding :)

Comments

Submitted bywonderman jacob (not verified)on Sat, 03/17/2018 - 05:40

Hello there how are you doing? this is just a test comment. Don't mind this. Google
Submitted byJohnDoen (not verified)on Sat, 03/11/2023 - 02:17

keep going.

Add new comment