• 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with arithmetic operations and active record

#1
Smile 
Hi guys Big Grin i need to make this query UPDATE `php_items` SET `item_quantity` = `item_quantity` - 10 WHERE `item_id` = 1 using the active record class and i have tried this:

$quantity = -10;
$data = array(
'item_quantity' => 'item_quantity'.$quantity,
);

$this->db->where('item_id', 1);
$this->db->update('php_items', $data);

But it returns: UPDATE `php_items` SET `item_quantity` = 'item_quantity-10' WHERE `item_id` = 1 What am i doind wrong?
Reply

#2
(10-30-2015, 03:55 PM)gustavo81988 Wrote: Hi guys Big Grin i need to make this query UPDATE `php_items` SET `item_quantity` = `item_quantity` - 10 WHERE `item_id` = 1 using the active record class and i have tried this:

   $quantity = -10;
   $data = array(
              'item_quantity' => 'item_quantity'.$quantity,
   );

   $this->db->where('item_id', 1);
   $this->db->update('php_items', $data);

But it returns: UPDATE `php_items` SET `item_quantity` = 'item_quantity-10' WHERE `item_id` =  1 What am i doind wrong?

You should disable auto-escape like following:

PHP Code:
$this->db->set('item_quantity''item_quantity - 10'FALSE);
$this->db->where('item_id'1);
$this->db->update('php_items'); 

Read more http://www.codeigniter.com/user_guide/da...ating-data
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.