Update Query in Codeigniter Using Where Condition

In this Codeigniter Update Query Tutorial – We would love to share with you how to update single or multiple records into database.

Here You will learn about update query with example. Generally we use the update query for updating a existing record into database.

Codeigniter Update Query

Content

  • Update Query
  • Single Record Update
  • Update Multiple Record
  • Set() Method
$sql = "update users SET name='tutsmake',email='[email protected]',mobile='888889999' where id='".1."'";
$query=$this->db->query($sql);

Single Record Update

Syntax :

$this->db->where('column_name','value');
$this->db->update('Table_name', $data);

You can use the below query for update single record into database.

$data = array( 
        'name'  = >  'tutsmake', 
        'contact_no'= > '8888899999', 
        'email'   = >  '[email protected]'
    );
$this->db->where('id', 1);
$this->db->update('users', $data);

Update Multiple Record

Syntax :

$this->db->update_batch('table_name', $array_of_data);

You can use the below query for update multiple record into database. You can use update_batch method for updating multiple record in single query.

$data = array( 
          array( 
            'id'   => '1';
            'name'  =>  'tutsmake', 
            'contact_no'=> '8888899999', 
            'email'   =>  '[email protected]'
        ),
        array( 
            'id'   => '2';
            'name'  = >  'tutsmake.com', 
            'contact_no'=> '8888899999', 
            'email'   =>  '[email protected]'
        ),
      ),
$this->db->update_batch('users', $data);

Update String

Syntax

$where = 'condition'; 
$this->db->insert_string('table_name', $data, $where);

This function simplifies the process of writing database updates. This gives the correctly formatted SQL updated string.

$data = array( 
        'name'  =>  'tutsmake', 
        'contact_no'=> '8888899999', 
        'email'   = >  '[email protected]'
    );
$where = "status = 'active'"; 
$str = $this->db->update_string('users', $data, $where);

Set() Method

You can also update existing record of database using codeigniter set() method. You can pass the array of column_name and value pair in set() function for update the record into database.

Syntax

$this->db->set(array);
$data = array(
    'name' => 'tutsmake',
    'email' => '[email protected]'
    );

$this->db->set($data)
$this->db->where('id', $id);
$this->db->update('users');

We hope, This example helpful for you.

If you have any query, please use comment box.

Spread the love

One Comment

  1. Deji said:

    great

    June 1, 2019
    Reply

Leave a Reply

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