Skip to main content

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