Search code examples
phpmysqlcodeigniterrecords

Updating a field subtract from a variable


what's the correct syntax in subtracting a field value from a variable value?

Example:

field1 = 100
variable1 = 10

I want to subtract variable1 from field1 using codeigniter's active records

my current code looks like this:

$this->db->set('volume', 'volume'-$r['quantity'], FALSE)
                    ->where('code',$r['ingredient_id'])
                    ->update('tbl_ingredients');
volume is the field
$r['quantity] is the variable

is this correct? because i get wrong results.


Solution

  • Try this

    $this->db->set('volume', 'volume-'.$r['quantity'], FALSE)