user3840898
user3840898

Reputation: 197

CodeIgniter UPDATE query to add a variable amount to a column value

I am executing an UPDATE query with variable onterpolation, but I want to use CodeIgniter's query builder methods.

$query = "update employee
          set employee_salary=employee_salary+ '" . $bones . '"
          where employee_id='" . $data['employee_id'] . "'
            and employee_code='" . $data['employee_code'] . "'";

i use the following code but does not work as below

$this->db->where('employee_id', $data['employee_id']);
$this->db->where('employee_code', $data['employee_code']);
$this->db->set('employee_salary', 'employee_salary+ $bones', FALSE);
$this->db->update('spar_m_in_out');

I want to update the employee salary by summing the current value with $bones.

Upvotes: 0

Views: 1076

Answers (2)

Rahul Kaushik
Rahul Kaushik

Reputation: 1464

You should update the third line as below:

$employee_salary  =   $employee_salary + $bones
$this->db->set('employee_salary', $employee_salary ), FALSE);

Upvotes: 2

Damien Pirsy
Damien Pirsy

Reputation: 25435

The variable isn't interpreted since it's inside single quotes, and gets passed down as is.

Concatenate the string so as to evaluate the variable and pass it as a whole string expression to the DB:

$this->db->set('employee_salary', 'employee_salary + '.$bones, FALSE);

Upvotes: 1

Related Questions