How to Edit /Update data on the database using CodeIgniter

  • Post author:
  • Post category:CodeIgniter
  • Post comments:0 Comments
  • Post last modified:October 5, 2020
  • Reading time:6 min(s) read

In this tutorial, we are going to learn Edit /Update data on the database using CodeIgniter. If you have not seen how to fetch data from the database using CodeIgniter.

If you are a beginner then I will suggest you go through all the CodeIgniter related blogs.

To edit a particular data we need to fetch their particular id from the database and send it through an anchor tag to a function where we want to send that particular id as shown in the below code:

<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Number</th>
<th>Created on</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($user_data as $key) {

?>
<tr>
<td><?php echo $key->name; ?></td>
<td><?php echo $key->number; ?></td>
<td><?php echo $key->created; ?></td>
<td>
<a href="<?php echo base_url('New_con/edit_data') ?>/<?php echo $key->id ; ?>" class="btn btn-info btn-xs">Edit</a>

<a href="" class="btn btn-danger btn-xs">Delete</a>
</td>

</tr>
<?php } ?>

</tbody>

</table>

</div>

</div>

Then, we need to create a particular function/method in the controller as shown in the below code:

public function edit_data($edit_id)
	{
		$data=$this->My_model->edit($edit_id);

		$capsule = array('e_data' => $data );

		$this->load->view('edit',$capsule);

	}

In the above code, we have set a parameter $edit_id to store the id of that particular data which has been sent through the anchor tag, then we simply pass the id to a function of our model.
After that, we need to create a function in the model to fetch the data with their respective id as shown in the code below:

public function edit($edit_id)
 	{ 
 $query=$this->db->where('id',$edit_id)->get('users')->result();

 		return $query;
 	}

Now we have to create an edit form in view folder(example:edit.php) where we can modify our data as shown below:

<!DOCTYPE html>
<html>
<head>
<title>CODEIGNITER FROM FOR EDIT DATA</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.0/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.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">


<div class="card">
<div class="card-header">My Edit Form</div>

<div class="card-body">

<form action="<?php echo base_url() ?>New_con/update_data" method="post">

<?php foreach($e_data as $key){ ?>

<input type="hidden" name="uid" value="<?php echo $key->id ;?>">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" value="<?php echo $key->name; ?>" class="form-control" placeholder="Enter Your Name" required>


</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Number</label>
<input type="tel" name="phone" value="<?php echo $key->number; ?>" class="form-control" placeholder="Enter Your phone number" required>


</div>
</div>
</div>
<?php } ?>
<input type="submit" name="submit" value="Save" class="btn btn-success form-group">
</form>
</div>
</div>


</div>

</body>
</html>

After modifying the data we need to update the data of that particular id into the database and that particular id will be our update id. That is why we have to send that update id through a hidden type input field which I have already highlighted in the above-mentioned code.

To update the data we need to create a function in the controller as shown in the below code:

public function update_data()
	{
		$uid=$this->input->post('uid');
		$name=$this->input->post('name');
		$phone=$this->input->post('phone');

		$capsule = array('name' =>$name ,'number'=>$phone );

		$data= $this->My_model->update($capsule,$uid);

		echo $data;

		$this->show_data();
	}

Now we need to create a function to update the data in our model as shown below;

public function update($capsule,$uid)
 	{
 		$update=$this->db->where('id',$uid)->update('users',$capsule);
 		if ($update) {
 		return $msg="Data Updated Successfully";
 		}
 	}
Here is the complete code of the controller and model how to Edit /Update data on the database using CodeIgniter as shown below:

New_con.php(Controller)

<?php
/**
* 
*/
class New_con extends CI_Controller
{

public function __construct()
{
parent:: __construct();

$this->load->helper('url');
$this->load->database();
$this->load->model('My_model');
}


public function index()
{
$this->load->view('a');
}


public function form()
{
$this->load->view('form');
}
public function data_insert()
{
$name=$this->input->post('name');
$phone=$this->input->post('phone');

$capsule = array('name' =>$name ,'number'=>$phone );

$msg=$this->My_model->save($capsule);

echo $msg;

$this->show_data();
}


public function show_data()
{

$data=$this->My_model->fetch();

$capsule = array('user_data' => $data );

$this->load->view('show',$capsule);


}

public function edit_data($edit_id)
{
$data=$this->My_model->edit($edit_id);

$capsule = array('e_data' => $data );

$this->load->view('edit',$capsule);

}

public function update_data()
{
$uid=$this->input->post('uid');
$name=$this->input->post('name');
$phone=$this->input->post('phone');

$capsule = array('name' =>$name ,'number'=>$phone );

$data= $this->My_model->update($capsule,$uid);

echo $data;

$this->show_data();
}
}


?>

My_model.php(model)

<?php 

/**
* 
*/
class My_model extends CI_Model
{

public function save($capsule)
{
$insert=$this->db->insert('users',$capsule);
if ($insert) {
return $msg="Data Inserted Successfully";
}
}




public function fetch()
{
$query=$this->db->order_by('id',"DESC")->get('users')->result();

return $query;



}

public function edit($edit_id)
{ 
$query=$this->db->where('id',$edit_id)->get('users')->result();

return $query;
}

public function update($capsule,$uid)
{
$update=$this->db->where('id',$uid)->update('users',$capsule);
if ($update) {
return $msg="Data Updated Successfully";
}
}

}

?>

Please leave your suggestion on the comment box. to support us follow our social media links. Subscribe our youtube channel BeAppend. Follow on Instagram like your Facebook page.

Leave a Reply