How to Update Database field value +1 in CodeIgniter

Use direct database Update instead of Model to increment a field +1

I always use the CodeIgniter models to do all the MySQL querys (insert, select, updates...) but this won't work if you want to update a count field +1.

Use this direct code in your Controller Function:

$this->db->set('my_field', 'my_field+1', FALSE); $this->db->where('my_other_field', 'test'); $this->db->update('my_table');
Code language: PHP (php)

About Ricard Torres

Senior Front-end Software Engineer at Netcentric, from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

@ricard_dev @ricard_dev

📝 Blog 🎙 Podcast

14 comments

Leave a Reply

Add <code> Some Code </code> if you need to.

*
*