update query in codeigniter using where condition

update query in codeigniter using where condition using Update Query, Single Record Update, Update Multiple Record and Set() Method.

Update Query

$sql = "update players SET name='tamilrokers',email='[email protected]',mobile='79855556565' where id='".1."'";
$query=$this->db->query($sql);

update query in codeigniter using where condition

Single Record Update

Example

$data = array( 
        'name'  = >  'tamilrokers', 
        'mobile_number'= > '79855556565', 
        'email'   = >  '[email protected]'
    );
$this->db->where('id', 1);
$this->db->update('players', $data);

Update Multiple Record

Example

$data = array( 
          array( 
            'id'   => '1';
            'name'  =>  'tamilrokers', 
            'mobile_number'=> '79855556565', 
            'email'   =>  '[email protected]'
        ),
        array( 
            'id'   => '2';
            'name'  = >  'tamilrokers.com', 
            'mobile_number'=> '79855556565', 
            'email'   =>  '[email protected]'
        ),
      ),
$this->db->update_batch('players', $data);

Update String

$data = array( 
        'name'  =>  'tamilrokers', 
        'mobile_number'=> '79855556565', 
        'email'   = >  '[email protected]'
    );
$where = "status = 'active'"; 
$str = $this->db->update_string('players', $data, $where);

update table in codeigniter

Update Query in Codeigniter using Where Condition

public function update_row(){
		
		$update_rows = array('field-name' => 'field-data',);
		$this->db->where('id', 1 );
		$this->db->update('table-name', $update_rows);	

	}	

simple update query in Codeigniter.

$result = "update players SET name='Rohan',email='[email protected]' where id='".1."'";
$qyt = $this->db->query($result);

Update using $this->db->update()

CodeIgniter Update Query

$data = array(
	'ply_code' => 'ply10',
	'ply_dept' => 'Cricket'
	);

$this->db->update('player_master',$data,'ply_ID=1');

Or as an array:

$this->db->update('player_master',$data,array('ply_ID' => 1));

Update with $this->db->where()

$data = array(
	'ply_code' => 'ply10',
	'ply_dept' => 'Cricket'
	);
$this->db->where('ply_ID', $id);
$this->db->update('player_master',$data);

Update with $this->db->set()

$data = array(
	'ply_code' => 'ply10',
	'ply_dept' => 'Cricket'
	);
$this->db->set($data)
$this->db->where('ply_ID', $id);
$this->db->update('player_master');

Update using $this->db->update_batch()

$data = array(
   array(
      'ply_ID' => 1 ,
      'ply_code' => 'ply10' ,
      'ply_dept' => 'Cricket'
   ),
   array(
      'ply_ID' => 2 ,
      'ply_code' => 'ply20' ,
      'ply_dept' => 'Cricket'
   )
);

$this->db->update_batch('player_master', $data, 'ply_ID');

// Produces:
// UPDATE `player_master` SET `ply_code` = CASE
// WHEN `ply_ID` = 1 THEN 'ply10'
// WHEN `ply_ID` = 2 THEN 'ply20'
// ELSE `ply_code` END,
// `ply_dept` = CASE
// WHEN `ply_ID` = 1 THEN 'Cricket'
// WHEN `ply_ID` = 2 THEN 'Cricket'
// ELSE `ply_dept` END
// WHERE `ply_ID` IN (1,2)

I hope you get an idea about update query in codeigniter using where condition.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment