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.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
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.
Random Code Snippet Queries: CodeIgniter
- How to call helper function in Codeigniter view
- How to move from one view to another in Codeigniter
- Get the version codeIgniter
- Get last executed query in codeigniter
- How to get field names of table in Codeigniter
- How to load a view in CodeIgniter?
- Unable to set session in codeigniter
- How to select all checkbox in Codeigniter
- Convert dd/mm/yyyy to yyyy-mm-dd in codeigniter
- How to create custom helper in codeigniter 4
- Default htaccess file code Codeigniter
- Codeigniter 4 call model function from controller
- Call to undefined function CodeIgniter\locale_set_default()
- No input file specified Codeigniter