In this example, We will learn Codeigniter update data in the database, Codeigniter update database record, Codeigniter update database, update data in Codeigniter example.
In this post, We will explain the step-by-step Codeigniter update data array using the Codeigniter update function. Also, I explain Codeigniter update record and update data by id in Codeigniter.
Step 1 : Create controller
Create a new application/controllers/Demo.php file and following the code.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Demo extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('demo_model');
$this->now_time = date('Y-m-d H:i:s');
}
function update_demo(){
$post_data['name'] = "Demo";
$post_data['email'] = "demo@gmail.com";
$post_data['updated_at'] = $this->now_time;
$result = $this->demo_model->update('demo',$post_data, array("id" => 1));
if($result){
echo "Demo updated";
}else{
echo "Demo not updated";
}
}
}
Step 2 : Create Model
Create a new application/models/Demo_model.php file and following the code. $this->db->update() function to update data in codeigniter.
<?php
class Demo_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
/**
* @param $table_name
* @param $data_array
* @param $where_array
* @return mixed
*/
function update($table_name,$data_array,$where_array){
$this->db->where($where_array);
$rs = $this->db->update($table_name, $data_array);
return $rs;
}
}
I hope you understand of codeigniter update data and it can help you..