CRUD Using Object Oriented Concept in PHP

OOP(Object Oriented Programming) is a programming pattern that uses real-world concepts such as object, inheritance, class, etc. OOP is best suitable for such a type of application that is scaleable. In this blog, we will learn CRUD operation using OOP. Let’s see the following steps:


Step1:
Create Database name oop:

CREATE DATABASE oop;


Stpe2:
Create a form table in the database:

CREATE TABLE form ( 
     id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, 
     name VARCHAR(100) NOT NULL, 
     email VARCHAR(255) NOT NULL, 
     phone  BIG INT(10) NOT NULL 
 );


Step3:
Create a Dbconnect.php file and save it in the classes folder, write the following code:

<?php 
   
 class Dbconnect 
 { 
  private $_localhost = 'localhost'; 
  private $_user = 'root'; 
  private $_password = ''; 
  private $_dbname = 'oop'; 
   
  protected $connection; 
   
   public function __construct() 
   { 
   
   if(!isset($this-> connection)) 
   { 
               
               $this->connection = new mysqli($this->_localhost , $this->_user , $this->_password , $this->_dbname); 
   
   } 
   
   return $this->connection; 
   
   } 
 } 
   
 ?> 


Step4:
Create a file Crud.php save in classes folder and write the following code:

<?php 
   
 include_once ('Dbconnect.php'); 
   
 class Crud extends Dbconnect 
 { 
             
             public     $columns=""; 
             public $values=""; 
             
             public $column=""; 
             public $value=""; 
             
             
             
             public function __construct() 
             { 
             parent::__construct();        
             } 
             
             
              public function selectalldata($table) 
   { 
                 $select="SELECT * FROM $table"; 
     $select1=$this->connection->query($select); 
     return $select1; 
   } 
             
             public function selectbyid($table,$id) 
             { 
                         $sel= "SELECT * FROM $table where id=$id"; 
                         $sel1=$this->connection->query($sel); 
                         return mysqli_fetch_array($sel1); 
                         
             } 
             
             public function insert($data,$table) 
             { 
                         
             //print_r($data); 
                         
                         foreach($data as $this->column => $this->value) 
                         { 
                                     
                                     $this->columns .= ($this->columns == "") ? "" : ", "; 
                                     $this->columns .= $this->column; 
                         
                                     $this->values .= ($this->values == "") ? "" : ", "; 
                                     $this->values .= "'".$this->value ."'"; 
                                     
                                     //echo $this->values; 
                         
                         } 
                         
                         $insert= ("INSERT into $table ($this->columns) values ($this->values)"); 
                         //echo $insert; 
                         $insert1= $this->connection->query($insert); 
                         
             } 
             
              public function update($data,$table,$id) 
     { 
              foreach ($data as $this->column => $this->value) 
      { 
      $update=("UPDATE $table SET $this->column = '$this->value' WHERE id= '$id'"); 
             // echo $update; 
              $this->connection->query($update); 
      } 
              return true; 
     } 
             
             function deletedata($table,$where) 
     { 
      $delete=("DELETE FROM $table WHERE id=$where"); 
              $this->connection->query($delete); 
              return true; 
     } 
             
             public function escape_string($value) 
             { 
                         return $this->connection->real_escape_string($value); 
             } 
             
 } 
   
   
 ?> 
   


Step5:
Create an add.php file and write the following code:

<?php 
   
 include_once ("classes/crud.php"); 
   
 $crud= new crud(); 
   
 if(isset($_POST['submit'])) 
 { 
             $data= array( 
             
                 "name"  => $crud->escape_string($_POST['name']),            
                         "email"  => $crud->escape_string($_POST['email']), 
                                                 "phone"  => $crud->escape_string($_POST['phone']) 
             
             ); 
             
             
               $crud->insert($data,'form'); 
             
             
             if($data) 
             { 
             echo 'insert successfully'; 
             header('location:listing.php'); 
             } 
             
 else 
             { 
             echo 'try again' ; 
             } 
             
             
 } 
   
   
 ?> 
 <!DOCTYPE html> 
 <html> 
 <head> 
 <title>add in oops</title> 
 <link rel="stylesheet" type="text/css" href="css/style.css"> 
 <link href="https://fonts.googleapis.com/css?family=Poppins&display=swap" rel="stylesheet"> 
 </head> 
 <body> 
   
   
 <form method="POST" name="form"> 
   
 <label>name</label> 
 <input type="text" name="name"><br/> 
   
 <label>email</label> 
 <input type="text" name="email"><br/> 
   
 <label>phone</label> 
 <input type="text" name="phone"><br/> 
   
 <input type="submit" name="submit"> 
   
 </form> 
 </body> 
 </html> 


Step6:
Create a listing.php file and write the following code:

<?php 
   
 include_once ("classes/crud.php"); 
    
  if(!empty($_GET['delid'])) 
  { 
   
  $id=$_GET['delid']; 
   
  $crud= new crud(); 
  $crud->deletedata("form",$id); 
  header('location:listig.php'); 
  } 
    
  ?> 
    
  <!DOCTYPE html> 
  <html lang="en"> 
  <head> 
    <title>Listing</title> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> 
  </head> 
  <body> 
    
  <div class="container"> 
        
    <table class="table table-striped"> 
      <thead> 
        <tr> 
          <th>Name</th> 
          <th>Mobile</th> 
          <th>Email</th> 
                          <th>Edit</th> 
                          <th>delete</th> 
        </tr> 
      </thead> 
      <tbody> 
              <?php 
    $crud= new crud(); 
     $result = $crud->selectalldata("form"); 
     
              while($data = mysqli_fetch_array($result)) 
              { 
              ?> 
              
        <tr> 
          <td><?php echo $data['name']; ?></td> 
          <td><?php echo $data['phone']; ?></td> 
          <td><?php echo $data['email']; ?></td> 
          <td><a href="edit.php?editid=<?php echo $data['id'];?>">edit</td> 
          <td><a href="listing.php?delid=<?php echo $data['id'];?>" onclick=" return confirm('Do You really want to delete this data')">delete</td> 
        </tr> 
              <?php } ?> 
      </tbody> 
    </table> 
  </div> 
    
  </body> 
  </html> 
    


Step7:
Create an edit.php file and write the following code:

<?php 
   
 include_once ("classes/crud.php"); 
   
 $id = $_GET['editid']; 
   
 $crud= new crud(); 
   
 $data = $crud->selectbyid('form',$id); 
   
 if(isset($_POST['submit'])) 
 { 
             $data= array( 
             
                 "name"  => $crud->escape_string($_POST['name']),            
                         "email"  => $crud->escape_string($_POST['email']), 
                                                 "phone"  => $crud->escape_string($_POST['phone']) 
             
             ); 
             
             
               $crud->update($data,'form',$id); 
             
             
             if($data) 
             { 
             echo 'updated successfully'; 
             header('location:listing.php'); 
             } 
             
 else 
             { 
             echo 'try again' ; 
             } 
             
             
 } 
   
   
   
   
 ?> 
 <!DOCTYPE html> 
 <html> 
 <head> 
 <title>Edit in oops</title> 
 <link rel="stylesheet" type="text/css" href="css/style.css"> 
 <link href="https://fonts.googleapis.com/css?family=Poppins&display=swap" rel="stylesheet"> 
 </head> 
 <body> 
   
   
 <form method="POST" name="form"> 
   
 <label>name</label> 
 <input type="text" name="name" value="<?php echo $data['name'];?>"><br/> 
   
 <label>email</label> 
 <input type="text" name="email" value="<?php echo $data['email'];?>"><br/> 
   
 <label>phone</label> 
 <input type="text" name="phone" value="<?php echo $data['phone'];?>"><br/> 
   
 <input type="submit" name="submit"> 
   
 </form> 
 </body> 
 </html> 
 


Note: 
In this blog, I have created a Crud.php file which is very useful to create an admin panel for website development, this is only a crud functionality for beginner developers to learn more about the object-oriented concept.

Download Source Code

Categories: web development php

Trending Courses

CodeIgniter

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Advance Digital Marketing

Regular : 6 Months

Fastrack : 3 Months

Crash : 2 Months

React JS

Regular : 45 Days

Fastrack : 25 Days

Crash : 15 Days

Laravel

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Front End Developer

Regular : 6 Months

Fastrack : 4 Months

Crash : 2 Months

Related Blogs

Request For Demo