Update Query in Codeigniter Using Where Condition

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='support@tutsmake.com',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'   = >  'support@tutsmake.com'
    );
$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'   =>  'support@tutsmake.com'
        ),
        array( 
            'id'   => '2';
            'name'  = >  'tutsmake.com', 
            'contact_no'=> '8888899999', 
            'email'   =>  'support-to@tutsmake.com'
        ),
      ),
$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'   = >  'support@tutsmake.com'
    );
$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' => 'test@gmail.com'
    );

$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.

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Codeigniter, Vue JS, Angular JS, React Js, WordPress and Bootstrap from a starting stage.

One reply to Update Query in Codeigniter Using Where Condition

  1. great

Leave a Reply

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