divHelper11
divHelper11

Reputation: 2208

Subtraction in SQL statement for stock

A database lists items and their stock count. When someone buys items I wish to update the stock count in the database.

Regardless of the following subtraction f_item_number = f_item_number - $mugAmount, my update statement isn't working.

If I have 200 mugs in the database. When I run the following statement the new mug amount is listed incorrectly afterwards as -1. Rather than the expected value of 199. Why is this?

DB::update('UPDATE `shop_items` SET `f_item_number` = ? WHERE `f_item_name` = ?', array(`f_item_number` - $mugAmount, "mug"));

Upvotes: 1

Views: 45

Answers (1)

Brett
Brett

Reputation: 2010

You can't pass in database column text in the parameters. Try this

DB::update('UPDATE `shop_items` SET `f_item_number` = `f_item_number` - ? WHERE `f_item_name` = ?', array($mugAmount, "mug"));

Upvotes: 2

Related Questions