How to subtract a number from a row in MySQL?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

How to subtract a number from a row in MySQL?

Post by MicroBoy »

I want that when I complet a form, the number in a row in database to subtract with the number that I wrote in the form.

p.s. I hope that I was cleared.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How to subtract a number from a row in MySQL?

Post by Christopher »

You can subtract using PHP variables or using SQL. Can you show some example code.
(#10850)
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: How to subtract a number from a row in MySQL?

Post by MicroBoy »

To be more clear. I have a table that show the equipments that I have, in that table is a field that show the quantity of an equipment. I have a form to sell the equipments. I want that the number of quantity that I submit at the form to subtract with the number of quantity in the table.

I'm using this, but it just subtract the number of the quantity at the first equipment, kodi is Code of each equipment, sasia is Quantity.

Code: Select all

 
.......
$kodii = $_POST[kodi];
$kodiii = $row['kodi'];
$a = $_POST[sasia];
$b = $row['sasia'];
$sasiaa = $b - $a;
 
if($kodii=$kodiii)
{
mysql_query("UPDATE pjeset SET sasia=" . $sasiaa . " WHERE kodi=" . $kodii. "");
 
.....
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How to subtract a number from a row in MySQL?

Post by Christopher »

I would recommend doing the subtraction in SQL because queries are atomic so there would be no danger of another update happening before this one with the old value. Also remember to sanitize your input.

Code: Select all

 $a = intval($_POST[sasia]);
$b = intval($row['sasia']);
 
if($kodii && ($b > 0))
{
mysql_query("UPDATE pjeset SET sasia=sasia-" . $b . " WHERE kodi=" . $kodii. "");
 
(#10850)
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: How to subtract a number from a row in MySQL?

Post by MicroBoy »

How to do in SQL? In the code that you wrote, you are subtracting the quanity with itself. Or i'm wrong?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How to subtract a number from a row in MySQL?

Post by Christopher »

Well then ... subtract the amount you want to reduce from the column.
(#10850)
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: How to subtract a number from a row in MySQL?

Post by MicroBoy »

Everytime the $row['sasia'] is the same, it shows just the information of the first row in database?
Post Reply