0

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.

0

2 Answers 2

2

You should update the third line as below:

$employee_salary  =   $employee_salary + $bones
$this->db->set('employee_salary', $employee_salary ), FALSE);
Sign up to request clarification or add additional context in comments.

2 Comments

yes it will work.. Try to debug using echo $this->db->last_query();
This answer's score is not indicative of its quality. This answer recommends clearly invalid code and flawed thinking. $employee_salary doesn't even exist, but the code is acting like the value has been pre-fetched from the database.
1

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.