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.
How to subtract a number from a row in MySQL?
Moderator: General Moderators
- 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?
You can subtract using PHP variables or using SQL. Can you show some example code.
(#10850)
Re: How to subtract a number from a row in MySQL?
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.
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. "");
.....
- 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?
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)
Re: How to subtract a number from a row in MySQL?
How to do in SQL? In the code that you wrote, you are subtracting the quanity with itself. Or i'm wrong?
- 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?
Well then ... subtract the amount you want to reduce from the column.
(#10850)
Re: How to subtract a number from a row in MySQL?
Everytime the $row['sasia'] is the same, it shows just the information of the first row in database?