• 18 Jan, 2025

How to Update Data in Database using Codeigniter

How to Update Data in Database using Codeigniter

How to Update Data in Database using Codeigniter

In this tutorial, we have handled Codeigniter Update opration using mysql,jQuery. fetch data form database & display Data  With html form in Codeigniter,  The Html form submit and update data  into MySQL database

Here put some sample code. for full source code click download now end of post

Step 1: Fetch data from MySQL database table .

Fetch data from database

sample code 

 

 $this->load->model('User_model');
$users = $this->User_model->allData();

 

 function allData()
{
 return $this->db->get('codeignitercrud')->result_array();
}

 

Step 2: Display data in list view .   
   
Display data in table view format

Screen short

DataTables AJAX Pagination with Search and Sorting in PHP1

Step 3: on click Edit button view data on update form using id (uniq key).

On click edit button via uniq key (primary key) get data with id wise

sample code
            

  

 $this->load->model('User_model');
 $user = $this->User_model->getUser($userId);
      
 function getUser($userId)
   {
     $this->db->where('id', $userId);
     return $this->db->get('codeignitercrud')->row_array();
   }


After getting data show on edit form

Screen short

How to Insert-update-delete Data in Database using Codeigniter1


Step 4: submit data update in to database server.  

on click edit submit data update data on database  

Edit function in application/controllers/user.php  

 $config['upload_path'] = './uploads/';
       $config['allowed_types'] = 'jpg|jpeg|png';
       date_default_timezone_set("Asia/Calcutta");
       $config['file_name'] = date('d-m-y_h-i-s');
       $this->load->library('upload', $config);
       if ($this->form_validation->run() == false) {
           $this->load->view('edit', $data);
       } else {
           $formArray = array(
               'email' => $this->input->post('email'),
               'firstname' => $this->input->post('firstname'),
               'lastname' => $this->input->post('lastname'),
               'address' => $this->input->post('address'),
               'gender' => $this->input->post('gender'),
               'hobbies' => implode(',', $this->input->post('hobbies')),
               'phonenumber' => $this->input->post('phonenumber'),
               'DOB' => $this->input->post('DOB')
           );
           if (!empty($_FILES['image']['name'])) {
               if (!empty($user['img'])) {
                   $oldImagePath = './uploads/' . $user['img'];
                   if (file_exists($oldImagePath)) {
                       unlink($oldImagePath);
                   }
               }
               if (!$this->upload->do_upload('image')) {
                   $data['upload_error'] = $this->upload->display_errors();
                   $this->load->view('edit', $data);
               }
               $upload_data = $this->upload->data();
               $formArray['img'] = $upload_data['file_name'];
           } else {
               $formArray['img'] = $user['img'];
           }
           $this->User_model->updateUser($userId, $formArray);

         

Screen short         

How to Insert-update-delete Data in Database using Codeigniter2    
Update query Inside model function with user id

 function updateUser($userId, $formArray)
   {
       $this->db->where('id', $userId);
       $this->db->update('codeignitercrud', $formArray);
   }


Download Full Code HERE   

Download Fome Here