CodeIgniter UPDATE Query – how to run UPDATE Query in CodeIgniter

CodeIgniter UPDATE Query – how to run UPDATE Query in CodeIgniter

In this post we will show you how to execute CodeIgniter UPDATE Query with update(), update_string() and update_batch().

CodeIgniter ‘UPDATE’ query execute by following functions

  • $this->db->update()
  • $this->db->update_string()
  • $this->db->update_batch()

CodeIgniter UPDATE Query with $this->db->update()

by using $this->db->update() method we can use UPDATE for single record and extecut CodeIgniter UPDATE Query.

//get user id
$user_id = $_POST['id'];
//get user name
$get_name = $_POST['name'];
//get user groupname
$get_groupname = $_POST['groupname'];
//get user age
$get_age = $_POST['age'];

$user_data = array(
    'name'      =>  $get_name ,
    'groupname' =>  $get_groupname,
    'age'       =>  $get_age 
);

$this->db->where('id', $user_id);
//update user data 
$this->db->update('tbl_all_user', $user_data);
// sql UPDATE query like 
// UPDATE tbl_all_user SET name = '$get_name', groupname = '$get_groupname', age = '$get_age' WHERE id = $user_id 

CodeIgniter UPDATE Query with $this->db->update_string()

by using $this->db->update_string() method we can use UPDATE for string type recoder/data and extecut UPDATE Query.

Note: Values are automatically escaped, producing safer queries.

$get_groupname = $_POST['groupname'];

$user_data = array(
    'groupname' => $get_groupname
);
$user_where = "status = 'user'";

//update user data 
$string = $this->db->update_string('tbl_all_user', $user_data, $user_where);

// sql UPDATE query like 
// UPDATE tbl_all_user SET groupname = '$get_groupname' WHERE id = $user_id 

CodeIgniter UPDATE Query with $this->db->update_batch()

by using $this->db->update_batch() method we can use UPDATE for mualtiple data and same time, with one execution and execute UPDATE Query. by using this method we can execute number of records with single execute query.

// pass user data in array 
$user_data = array(
   array(
    'name'  = >  'add_anme_1' ,
    'groupname'= >  'add_your_group_name1',
    'age'   = >  '24'
   ),
   array(
    'name'  = >  'add_anme_2' ,
    'groupname'= >  'add_your_group_name2',
    'age'   = >  '25'
   )
);
//update user data 
$this->db->update_batch('tbl_all_user', $user_data);

// sql UPDATE mulatiple data in query like  
// UPDATE tbl_all_user SET name = '$get_name', groupname = '$get_groupname', age = '$get_age' WHERE id = $user_id 

You also like codeigniter select query and codeigniter join tables

Leave a Comment

Your email address will not be published. Required fields are marked *

76  +    =  85

We accept paid advance and paid guest Posting on our Site : Contact US