CRUD In CodeIgniter

In this blog, we will learn CRUD operation using CodeIgniter. In CodeIgniter to use crud operation, we have to place our code according to the MVC pattern. We will keep our design part in views, logics in controller and database-related code in models. Let’s see the following steps to use crud:


Step1:
Download the CodeIgniter3 Project from its official website, extract it and place it in your xampp directory.

https://codeigniter.com/download

Step2:
 Setup basic configuration in your project like base URL in config.php file, autoload necessary libraries, helper in autoload.php file.
 $config['base_url'] = 'http://localhost/codeigniter-crud'; 
 $autoload['helper'] = array('url'); 
 $autoload['model'] = array('crud'); 


Step3:  
Create Database named codeigniter_crud

CREATE DATABASE codeigniter_crud 


Step4:
Setup Database Connection in application>config>database.php file.

$db['default'] = array( 
     'dsn'   => '', 
     'hostname' => 'localhost', 
     'username' => 'root', 
     'password' => '', 
     'database' => 'codeigniter_crud', 
     'dbdriver' => 'mysqli', 
     'dbprefix' => '', 
     'pconnect' => FALSE, 
     'db_debug' => (ENVIRONMENT !== 'production'), 
     'cache_on' => FALSE, 
     'cachedir' => '', 
     'char_set' => 'utf8', 
     'dbcollat' => 'utf8_general_ci', 
     'swap_pre' => '', 
     'encrypt' => FALSE, 
     'compress' => FALSE, 
     'stricton' => FALSE, 
     'failover' => array(), 
     'save_queries' => TRUE 
 ); 
   


Step5:
 Make clean or seo friendly url using routes.php file of CodeIgniter , write the following code:


 $route['default_controller'] = 'welcome'; 
 $route['404_override'] = ''; 
 $route['translate_uri_dashes'] = FALSE; 
   
   
 $route['add'] = 'welcome/add'; 
 $route['edit/(:num)'] = 'welcome/edit/$1'; 
 $route['delete/(:num)'] = 'welcome/delete/$1'; 
   


Step6:
 Create add.php in views folder, write the following code:

<!DOCTYPE html> 
 <html lang="en"> 
 <head> 
   <title>Insert</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"> 
   <h2>Insert</h2> 
   <form method="post"> 
     <div class="form-group"> 
       <label for="email">Name:</label> 
       <input type="text" class="form-control"  placeholder="Enter name" name="name"> 
     </div> 
     <div class="form-group"> 
       <label for="pwd">Email:</label> 
       <input type="email" class="form-control"  placeholder="Enter email" name="email"> 
     </div> 
             
              <div class="form-group"> 
       <label for="pwd">Mobile:</label> 
       <input type="text" class="form-control"  placeholder="Enter mobile" name="mobile"> 
     </div> 
             
    
     <button type="submit" name="submit" class="btn btn-primary">Submit</button> 
   </form> 
 </div> 
   
 </body> 
 </html> 
   


Step7:
Create listing.php file in views folder, write the following code:

<!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"> 
   <h2>Listing Data</h2> 
 <p><a href="<?php echo base_url('add');?>" class="btn btn-primary">Add</a></p> 
   <table class="table table-bordered"> 
     <thead> 
       <tr> 
         <th>Name</th> 
         <th>Email</th> 
         <th>Mobile</th> 
                           <th>Edit</th> 
                             <th>Delete</th> 
       </tr> 
     </thead> 
     <tbody> 
             
             <?php foreach($LISTDATA as $data) 
             {?> 
       <tr> 
         <td><?php echo $data->name;?></td> 
         <td><?php echo $data->email;?></td> 
         <td><?php echo $data->mobile;?></td> 
                         <td><a href="<?php echo base_url('edit/'.$data->id);?>">Edit</a></td> 
                         <td><a href="<?php echo base_url('delete/'.$data->id);?>">Delete</a></td> 
       </tr> 
             <?php } ?> 
     </tbody> 
   </table> 
 </div> 
   
 </body> 
 </html> 
   


Step8:  
Create edit.php file in views folder, write the following code:

<!DOCTYPE html> 
 <html lang="en"> 
 <head> 
   <title>Edit</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"> 
   <h2>Update</h2> 
   <form method="post"> 
     <div class="form-group"> 
       <label for="email">Name:</label> 
       <input type="text" class="form-control" value="<?php echo $EDITDATA[0]->name;?>"  placeholder="Enter name" name="name"> 
     </div> 
     <div class="form-group"> 
       <label for="pwd">Email:</label> 
       <input type="email" class="form-control" value="<?php echo $EDITDATA[0]->email;?>"  placeholder="Enter email" name="email"> 
     </div> 
             
              <div class="form-group"> 
       <label for="pwd">Mobile:</label> 
       <input type="text" class="form-control" value="<?php echo $EDITDATA[0]->mobile;?>"  placeholder="Enter mobile" name="mobile"> 
     </div> 
             
    
     <button type="submit" name="submit" class="btn btn-primary">Submit</button> 
   </form> 
 </div> 
   
 </body> 
 </html> 
   


Step9:
Create Crud.php file in models folder,write the following code:

<?php 
 class Crud extends CI_Model{ 
     
     
 function insert($table,$data) 
             { 
                         $result= $this->db->insert($table, $data); 
                         return $result; 
             } 
             
             
             
             function update($table,$id,$data) 
             { 
                         $this->db->where('id', $id); 
                         return $this->db->update($table,$data); 
             } 
   
   
             
             function get_data($table) 
             {   
                $data= $this->db->get($table); 
                return $data->result();                
             } 
   
             
             function delete($table,$id) 
             { 
                         $this->db->where('id', $id); 
                         return $this->db->delete($table); 
             } 
             
   
             public function fetchdatabyid($id,$table) 
     { 
                                     $this->db->where('id',$id); 
                 $data=$this->db->get($table); 
                 return $data->result(); 
     } 
   
             
   
             
 } 
   


Step10: 
In default controller, Welcome.php of CodeIgniter, write the following code:

<?php 
 defined('BASEPATH') OR exit('No direct script access allowed'); 
   
 class Welcome extends CI_Controller { 
   
             /** 
              * Index Page for this controller. 
              * 
              * Maps to the following URL 
              *                      http://example.com/index.php/welcome 
              *          - or - 
              *                      http://example.com/index.php/welcome/index 
              *          - or - 
              * Since this controller is set as the default controller in 
              * config/routes.php, it's displayed at http://example.com/ 
              * 
              * So any other public methods not prefixed with an underscore will 
              * map to /index.php/welcome/<method_name> 
              * @see https://codeigniter.com/user_guide/general/urls.html 
              */ 
             public function index() 
             { 
                         $data['LISTDATA']=$this->crud->get_data('registration'); 
                         $this->load->view('listing',$data); 
             } 
             
             public function add() 
             { 
                         if(isset($_POST['submit'])) 
                         { 
                                     $data['name']=$this->input->post('name'); 
                                     $data['email']=$this->input->post('email'); 
                                     $data['mobile']=$this->input->post('mobile'); 
                                     
                                     $this->crud->insert('registration',$data); 
                                     redirect(base_url()); 
                         } 
                         $this->load->view('add'); 
             } 
             
             public function edit() 
             { 
                         $args=func_get_args(); 
                         
                         if(isset($_POST['submit'])) 
                         { 
                                     $data['name']=$this->input->post('name'); 
                                     $data['email']=$this->input->post('email'); 
                                     $data['mobile']=$this->input->post('mobile'); 
                                     
                                     $this->crud->update('registration',$args[0],$data); 
                                     redirect(base_url()); 
                         } 
                         $data['EDITDATA']=$this->crud->fetchdatabyid($args[0],'registration'); 
                         $this->load->view('edit',$data); 
             } 
             
             public function delete() 
             { 
                $args=func_get_args(); 
                
                $this->crud->delete('registration',$args[0]); 
                
                redirect(base_url()); 
                
             } 
             
 }

Download Source Code

Categories: web development codeigniter

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