Update Query in Codeigniter using Where Condition

In this tutorial, We would love to share with you how to update single or multiple records in database table.

How to use Update Query in Codeigniter using Where Condition

Here are:

  • Query() Method
  • db->update() to Update Single Record
  • db->update_batch() to Update Multipe Record
  • Set() Method

Query() Method

In Ci, the Query() method allows users to execute raw queries to update,read,insert and delete data in the database:

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

db->update() to Update Single Record

In Codeigniter, the UPDATE() clause allows updating data in a database table:

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

Here is an example to update single record in database:

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

db->update_batch() to Update Multipe Record

The update_batch() clause method allows users to update multiple records in a database table:

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

Here is an example of update_batch method for updating multiple record:

$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);

Set() Method

Updating existing records of database table using CodeIgniter set() method.

$this->db->set(array);

Here is an example of set() method for update data:

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

Recommended Tutorials

If you have any query, please use comment box.

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

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 *