Update Query in Codeigniter Using Where Condition
Codeigniter – In this tutorial, we’ll show you how to update one or more records in your database using Codeigniter. We’ll explain the process with examples to help you understand better.
Updating existing records in a database is a common task, and we use an update query for this purpose. So, let’s get started and learn how to do it with Codeigniter!
Codeigniter Update Query in Codeigniter Using Where Condition
Codeigniter Update Query
Contents
- 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' = > 'torqueprogramming',
'contact_no'= > '99999-99999',
'email' = > 'support@torqueprogramming.co.in'
);
$this->db->where('id', 1);
$this->db->update('users', $data);
Update Multiple Record
Syntax :
$this->db->update_batch('table_name', $array_of_data);
If you want to update multiple records in your database, there’s a simple query you can use. Just use the update_batch method, and you can update multiple records with a single query.
$data = array(
array(
'id' => '1';
'name' => 'torqueprogramming',
'contact_no'=> '8888899999',
'email' => 'support@torqueprogramming.co.in'
),
array(
'id' => '2';
'name' = > 'torqueprogramming.co.in',
'contact_no'=> '8888899999',
'email' => 'info@torqueprogramming.co.in'
),
),
$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' => 'torqueprogramming',
'contact_no'=> '8888899999',
'email' = > 'support@torqueprogramming.co.in'
);
$where = "status = 'active'";
$str = $this->db->update_string('users', $data, $where);
Set() Method
In Codeigniter, you can also update existing records using the set() method. Just pass an array of column names and their corresponding values to the set() function. This will update the record in your database with the new values.
Syntax
$this->db->set(array);
$data = array(
'name' => 'torqueprogramming',
'email' => 'test@gmail.com'
);
$this->db->set($data)
$this->db->where('id', $id);
$this->db->update('users');
We hope, This example helpful for you.
You May Also Like This:-

