1

I want to add 1 to my column named SIZE (integer), this is my query:

$this->dbsections->update('sections', "SIZE = SIZE + 1");

But in the error message, it reads as:

UPDATE `sections` SET `SIZE = SIZE +` 1 = '' WHERE `NAME` = 'ABC'
2
  • I can't remember off hand, can you do $this->dbsections->update('sections', ['size' => 'size + 1']); I have a feeling this won't work and you might need to write the query in a different way, as a full query Commented May 12, 2016 at 13:49
  • Yes, it didn't worked Commented May 12, 2016 at 13:50

2 Answers 2

3

You can rewrite your update query

$this->dbsections->set('SIZE', 'SIZE+1', FALSE);// third parameter FALSE
$this->dbsections->where('NAME', "ABC");
$this->dbsections->update('sections');

set() will also accept an optional third parameter ($escape), that will prevent data from being escaped if set to FALSE

Read https://www.codeigniter.com/userguide3/database/query_builder.html#updating-data

Sign up to request clarification or add additional context in comments.

Comments

2

You can try this:

$this->dbsections->query("UPDATE sections SET SIZE = SIZE + 1 WHERE NAME = 'ABC'");

3 Comments

NAME is a MySQL reserved word so might need escaping. Whilst this does answer the specific question asked, if other fields needed updating also, the query string itself could get quite large and unreadable if not formatted neatly; as a result there are better ways of doing this
@gabe3886 NAME is keyword in mysql not reserved check there is no (R) after name!! dev.mysql.com/doc/refman/5.7/en/keywords.html
@Saty My mistake, I misread the documentation. Thanks for pointing it out. It's probably still worth escaping the column names though, just in case that changes in a future version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.