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
Delete Query in PHP Codeigniter Framework
Codeigniter Single & Multiple Insert Query
If you have any query, please use comment box.
great