Update an existing table record with +1 in CodeIgniter

Created at 20-Mar-2021 , By samar

Update an existing table record with +1 in CodeIgniter

Through many examples, we will learn how to resolve the "Update an existing table record with +1 in CodeIgniter".

  • --PATH application\controllers\<yourcontroller>.php
    $this->db->set('views', 'views + 1',FALSE); 
    $this->db->where('id', $id); 
    $this->db->update('posts');
    
    You can update an existing table record with +1 in CodeIgniter using set() method. $this->db->set() method set value for column for insert or update query. $this->db->where() function enables you to set WHERE clause on query. set() method will also accept an optional third parameter ($escape), that will prevent data from being escaped if set to FALSE. which is used in our case and it generates a query like UPDATE posts SET views = views+1 WHERE id = 2. If the third parameter of set() method is not passed or passed as TRUE then it returns a query like UPDATE `posts` SET `views` = 'views+1' WHERE `id` = 2. You have to set FALSE to increment the value of the column of a record.

Back to code snippet queries related codeIgniter

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.