2

i want to increment my field value to its current value plus 1 in my codeigniter project. so, i made one function but it is not working. my function is.

function increse_field_by_1($table_name,$fieldToIncrease,$whileCondition){

$this->db->where($whileCondition);
$this->db->set($fieldToIncrease, $fieldToIncrease+1, FALSE);
$this->db->update($table_name); 
$query = $this->db->get();

}

and its displaying error like.

Error Number: 1054

Unknown column '$fieldToIncrease' in 'field list'

UPDATE `rule` SET ruleid = $fieldToIncrease+1 WHERE `interface` = 'lan'

i don't know how to resolve it. please help me.

Divyesh Jesadiya
  • 1,105
  • 4
  • 30
  • 68
  • try this: https://stackoverflow.com/questions/6373564/increment-field-of-mysql-database-using-codeigniters-active-record-syntax – Jigar Shah Jun 23 '17 at 06:59
  • how and where do you call increse_field_by_1 ? - show this type of code pls – Atural Jun 23 '17 at 06:59

1 Answers1

2

you tried to increment the value in php try this instead

function increse_field_by_1($table_name,$fieldToIncrease,$whileCondition){

    $this->db->where($whileCondition);
    $this->db->set($fieldToIncrease, $fieldToIncrease."+1", FALSE);
    $this->db->update($table_name); 
}

Your $this->db->get() call isn't needed because update does the job already. Just remove this line.

For more information take a look here

Atural
  • 5,389
  • 5
  • 18
  • 35