CRUD Using Function in PHP

CRUD (Create, Read, Update, Delete) is an important topic of PHP in which you will learn how to insert, select, update and delete data. In this Blog, we will learn CRUD operation by using function. The function is used when some functionality is used multiple times in a project like a crud operation which is used multiple times in the project, Let’s see the following steps:

Step1: Create Database name crud:

CREATE DATABASE crud;


Step2:
Create a registration table in the database:

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


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

<?php 
 $servername = "localhost"; 
 $username = "root"; 
 $password = ""; 
 $db="crud"; 
   
 // Create connection 
 $connect = mysqli_connect($servername, $username, $password,$db); 
   
 // Check connection 
 if (!$connect) { 
   die("Connection failed: " . mysqli_connect_error()); 
 } 
   
 ?> 


Step4:
Create a file all-function.php save in includes folder and write the following code:

<?php 
   
  function selectalldata($table) 
  { 
                $select="SELECT * FROM $table"; 
    $select1=mysqli_query($GLOBALS['connect'],$select); 
    return $select1; 
  } 
   
   function selectdatabyid($table,$id) 
    { 
    $select="SELECT * FROM $table where id= $id"; 
    $select1=mysqli_query($GLOBALS['connect'],$select); 
    return mysqli_fetch_array($select1); 
   
    } 
   
  function insert($data,$table) 
    { 
         $columns = ""; 
                         $values = ""; 
                         
                         foreach ($data as $column => $value) { 
                                     
                                     $columns .= ($columns == "") ? "" : ", "; 
                                     
                                     $columns .= $column; 
                                     
                                     $values .= ($values == "") ? "" : ", "; 
                                     $values .= $value; 
                         } 
                         
                         //echo $columns; 
                         //echo $values; 
                         
             $insert=("INSERT INTO $table ($columns) VALUES ($values)"); 
             //echo $insert; 
             mysqli_query($GLOBALS['connect'],$insert); 
    } 
    
    
     function update($data,$table,$where) 
    { 
             foreach ($data as $coloum => $value) 
     { 
     $update=("UPDATE $table SET $coloum = $value WHERE id= '$where'"); 
             //echo $update; 
             mysqli_query($GLOBALS['connect'],$update); 
     } 
             return true; 
    } 
    
    function deletedata($table,$where) 
    { 
     $delete=("DELETE FROM $table WHERE id=$where"); 
             mysqli_query($GLOBALS['connect'],$delete) or die(mysqli_error()); 
             return true; 
    } 
    
    ?>


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

<?php 
   
 include("includes/connection.php"); 
 include("includes/allfunction.php"); 
   
 if(isset($_POST['submit'])) 
 { 
              $data=array( 
                                                 "name"=>"'".$_POST['name']."'", 
                                                 "email"=>"'".$_POST['email']."'", 
                                                 "mobile"=>"'".$_POST['mobile']."'" 
                                                 ); 
               
               insert($data,'registration'); 
             header('location:listing.php'); 
             
 } 
   
 ?> 
   
 <!DOCTYPE html> 
 <html> 
 <head> 
 </head> 
   
 <body> 
   
 <form method="POST"> 
   
 <input type="text" name="name" placeholder="enter name"> 
 <input type="text" name="email" placeholder="enter email"> 
 <input type="text" name="mobile" placeholder="enter mobile"> 
   
 <input type="submit" name="submit"> 
   
 </form> 
   
 </body> 
 </html> 
   


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

<?php 
 include("includes/connection.php"); 
 include("includes/allfunction.php"); 
   
 if(!empty($_GET['delid'])) 
 { 
 $id=$_GET['delid']; 
   
 deletedata("registration",$id); 
 } 
   
 ?> 
   
 <!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 
   
    $result = selectalldata("enquiry"); 
    
             while($data = mysqli_fetch_array($result)) 
             { 
             ?> 
             
       <tr> 
         <td><?php echo $data['name']; ?></td> 
         <td><?php echo $data['mobile']; ?></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("includes/connection.php"); 
 include("includes/allfunction.php"); 
   
 $id =$_GET['editid']; 
 $data = selectdatabyid("enquiry",$id); 
   
 if(isset($_POST['submit'])) 
 { 
              $data=array( 
                                                 "name"=>"'".$_POST['name']."'", 
                                                 "email"=>"'".$_POST['email']."'", 
                                                 "mobile"=>"'".$_POST['mobile']."'", 
                                                 "subject"=>"'".$_POST['subject']."'", 
                                                 "message"=>"'".$_POST['message']."'" 
                                                 ); 
               
               update($data,'enquiry',$id); 
             
             header("location:listing.php"); 
             
 } 
   
 ?> 
   
 <!DOCTYPE html> 
 <html> 
 <head> 
 </head> 
   
 <body> 
   
 <form method="POST"> 
   
 <input type="text" name="name" value="<?php echo $data['name'];?>" placeholder="enter name"> 
 <input type="text" name="email" value="<?php echo $data['email'];?>" placeholder="enter email"> 
 <input type="text" name="mobile" value="<?php echo $data['mobile'];?>" placeholder="enter mobile"> 
   
 <input type="text" name="subject" value="<?php echo $data['subject'];?>" placeholder="enter subject"> 
   
 <input type="text" name="message" value="<?php echo $data['message'];?>" placeholder="enter message"> 
   
 <input type="submit" name="submit"> 
   
 </form> 
   
 </body> 
 </html> 
  


Note: 
In this crud example I have created an all-functions.php file which is very useful to create an admin panel for website development. In this we had not used any data filter methods for security, this is only a crud functionality for beginner developers to learn more about function.

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 : 20 Days

Crash : 7 Days

Laravel

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Front End Developer

Regular : 7 Month

Fastrack : 4 Month

Crash : 2 Month

Related Blogs

Request For Demo