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='[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.

AuthorAdmin

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 *