PHP Codeigniter Tutorial: CRUD Example App with Bootstrap 4 and MySQL Database

Today, We want to share with you codeigniter crud.In this post we will show you edit and update data in codeigniter, hear for Codeigniter 3 – Basic CRUD application with MySQL Example with Demo we will give you demo and example for implement.In this post, we will learn about update record in codeigniter with an example.

how to add edit delete in codeigniter?

Database and Configuration (members table:)

CREATE TABLE IF NOT EXISTS `members` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `member_bios` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=16 ;

application/config/database.php

 '',
	'hostname' => 'localhost',
	'username' => 'pakainfo_v1',
	'password' => 'Pakjd@34jdfjkjk8956',
	'database' => 'demo_example',
	'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
);

Create Routes

application/config/routes.php


Add MemberInfo Controller (application/controllers/MemberInfo.php)

load->library('form_validation');
      $this->load->library('session');
      $this->load->model('MemberInfoModel');


      $this->memberInfo = new MemberInfoModel;
   }


   /**
    * Display Data this method.
    *
    * @return Response
   */
   public function index()
   {
       $data['data'] = $this->memberInfo->get_memberInfo();


       $this->load->view('theme/header');       
       $this->load->view('memberInfo/list',$data);
       $this->load->view('theme/footer');
   }


   /**
    * Show Details this method.
    *
    * @return Response
   */
   public function show($id)
   {
      $member = $this->memberInfo->find_member($id);


      $this->load->view('theme/header');
      $this->load->view('memberInfo/show',array('member'=>$member));
      $this->load->view('theme/footer');
   }


   /**
    * Create from display on this method.
    *
    * @return Response
   */
   public function create()
   {
      $this->load->view('theme/header');
      $this->load->view('memberInfo/create');
      $this->load->view('theme/footer');   
   }


   /**
    * Store Data from this method.
    *
    * @return Response
   */
   public function store()
   {
        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('member_bios', 'Description', 'required');


        if ($this->form_validation->run() == FALSE){
            $this->session->set_flashdata('errors', validation_errors());
            redirect(base_url('memberInfo/create'));
        }else{
           $this->memberInfo->insert_member();
           redirect(base_url('memberInfo'));
        }
    }


   /**
    * Edit Data from this method.
    *
    * @return Response
   */
   public function edit($id)
   {
       $member = $this->memberInfo->find_member($id);


       $this->load->view('theme/header');
       $this->load->view('memberInfo/edit',array('member'=>$member));
       $this->load->view('theme/footer');
   }


   /**
    * Update Data from this method.
    *
    * @return Response
   */
   public function update($id)
   {
        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('member_bios', 'Description', 'required');


        if ($this->form_validation->run() == FALSE){
            $this->session->set_flashdata('errors', validation_errors());
            redirect(base_url('memberInfo/edit/'.$id));
        }else{ 
          $this->memberInfo->update_member($id);
          redirect(base_url('memberInfo'));
        }
   }


   /**
    * Delete Data from this method.
    *
    * @return Response
   */
   public function delete($id)
   {
       $member = $this->memberInfo->delete_member($id);
       redirect(base_url('memberInfo'));
   }
}

application/models/MemberInfo.php

input->get("search"))){
          $this->db->like('title', $this->input->get("search"));
          $this->db->or_like('member_bios', $this->input->get("search")); 
        }
        $query = $this->db->get("members");
        return $query->result();
    }


    public function insert_member()
    {    
        $data = array(
            'title' => $this->input->post('title'),
            'member_bios' => $this->input->post('member_bios')
        );
        return $this->db->insert('members', $data);
    }


    public function update_member($id) 
    {
        $data=array(
            'title' => $this->input->post('title'),
            'member_bios'=> $this->input->post('member_bios')
        );
        if($id==0){
            return $this->db->insert('members',$data);
        }else{
            $this->db->where('id',$id);
            return $this->db->update('members',$data);
        }        
    }


    public function find_member($id)
    {
        return $this->db->get_where('members', array('id' => $id))->row();
    }


    public function delete_member($id)
    {
        return $this->db->delete('members', array('id' => $id));
    }
}
?>

application/views/theme/header.php




    Basic Crud operation in Codeigniter 3 - www.pakainfo.com
    


application/views/theme/footer.php

  

application/views/memberInfo/list.php

Codeigniter 3 CRUD Example from scratch

Title Description Action
title; ?> member_bios; ?>
show Edit

application/views/memberInfo/create.php

Add New Member

session->flashdata('errors')){ echo '
'; echo $this->session->flashdata('errors'); echo "
"; } ?>
Member Title:
Member Description:

application/views/memberInfo/show.php

Show Member

Title: title; ?>
Member Description: member_bios; ?>

application/views/memberInfo/edit.php

Edit Member

session->flashdata('errors')){ echo '
'; echo $this->session->flashdata('errors'); echo "
"; } ?>
Title:
Member Description:

I hope you get an idea about Codeigniter 3 CRUD (Create,Read,Update,Delete) via Mysql.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment