CodeIgniter Update Data

In this section, We'll understand how to update records. We'll use tbl_users table to update the records.

Using a text editor, copy the below code in your controller called User.php. In it, place this code and save it to your application/controllers/ directory:

<?php
  
   class User extends CI_Controller {
   
      public function __construct() { 
         parent::__construct(); 
         $this->load->helper(array('form', 'url')); 
      } 
	
      public function index() {
			
         /* Load form validation library */ 
         $this->load->library('form_validation');
			
	 /* Validation rule */
	 $this->form_validation->set_rules('name', 'Name', 'required');
	 $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
	 $this->form_validation->set_rules('contact_no', 'Contact Number', 'required');		 
			
         if ($this->form_validation->run() == FALSE) { 
            $this->load->view('user_form'); 
         } 
         else { 
                    $this->load->model('user_model');
		    $this->user_model->save();
		    $success = "Submit successfully!";
                    $this->load->view('user_form', compact('success')); 
         } 
      }
	  
     public function edit($id) {
			
         /* Load form validation library */ 
         $this->load->library('form_validation');
			
	 /* Validation rule */
	 $this->form_validation->set_rules('name', 'Name', 'required');
	 $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
	 $this->form_validation->set_rules('contact_no', 'Contact Number', 'required');		 
			
         if ($this->form_validation->run() == FALSE) { 
            $this->load->view('user_edit'); 
         } 
         else { 
                    $this->load->model('user_model');
		    $this->user_model->update($id);
		 }
	 }
	  
	  public function getdata()
	     {
		   $this->load->model('user_model');
		   $data['record'] = $this->user_model->getdata();
		   $this->load->view('display_users', $data);
	     }
		 
	   public function getrecordbyid($id)
	     {
		   $this->load->model('user_model');
		   $data['record'] = $this->user_model->getuserinfobyid($id);
		   $this->load->view('user_edit', $data);
	     }
  }
?>

Using a text editor, create a new view file called user_edit.php. In it, place this code and save it to your application/views/ directory:

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <div class="col-md-2"></div>
     <div class="col-md-8" style="margin-top:20px">
	    <?php
		   echo form_open('user/edit/'.$record->id);
		   echo validation_errors();
		   if (isset($success))
		   echo '<p>'.$success.'</p>';
	    ?>
		<div class="form-group">
			<label for="email">Name:</label>
			<input type="text" id="name" name="name" value="<?php echo $record->name; ?>" />
		</div>
		<div class="form-group">
			<label for="email">Email:</label>
			<input type="text" id="email" name="email" value="<?php echo $record->email; ?>" />
		</div>
		<div class="form-group">
			<label for="contact_no">Contact Number:</label>
			<input type="text" id="contact_no" name="contact_no" value="<?php echo $record->contact_no; ?>" />
		</div>
		<button type="submit" class="btn btn-success">Update</button>
		<?php 
		echo form_close(); 
		?>
		</div>
	 <div class="col-md-2"></div>
	</body>
</html>

Using a text editor, copy the below code in your model called User_model.php. In it, place this code and save it to your application/models/ directory:

<?php
class User_model extends CI_Model
{
    public function save()
    {
	   $data['name'] = $this->input->post('name');
	   $data['email'] = $this->input->post('email');
	   $data['contact_no'] = $this->input->post('contact_no');
	   $this->db->insert('tbl_users', $data);
	   }
	   
	   public function getdata()
	     {
		    return $this->db->get('tbl_users')->result();	
	     }
		 
	   public function getuserinfobyid($id)
	     {
		    return $this->db->where('id', $id)->get('tbl_users')->row();
	     }
		 
    public function update($id)
    {
	   $data['name'] = $this->input->post('name');
	   $data['email'] = $this->input->post('email');
	   $data['contact_no'] = $this->input->post('contact_no');
	   $this->db->where('id', $id)->update('tbl_users', $data);
	   redirect('user/getdata');
	}		 
		 
  }

To try to update, click on the edit link displayed in the listing and update the text field value

codeigniter update data into database codeigniter update data into database codeigniter update data into database