petenaylor Posted November 25, 2010 Share Posted November 25, 2010 Hi all I have an SQL table with a quantity in and I am trying to write a piece of code to reduce the quantity by 1 each time. Here's my code: $queryc = mysql_query(" SELECT FROM `voucher_codes` WHERE name = '".$voucherc."'"); $resultc = mysql_fetch_array ($queryc); $voucherqtyvalue = $resultc['qty']; $newvoucherqty = $voucherqtyvalue - 1; $queryi = mysql_query(" UPDATE `voucher_codes` SET qty = '".$newvoucherqty."' WHERE name = '".$_SESSION['vouchercode']."'"); $resulti = mysql_result($queryi); Instead, this changes the value to -1 in the table. Please help! Cheers Pete Quote Link to comment https://forums.phpfreaks.com/topic/219813-reducing-sql-value-by-one/ Share on other sites More sharing options...
laffin Posted November 25, 2010 Share Posted November 25, 2010 made much simpler with 1 query UPDATE table SET qty=qty-1 WHERE name=vouchercode [/code Quote Link to comment https://forums.phpfreaks.com/topic/219813-reducing-sql-value-by-one/#findComment-1139500 Share on other sites More sharing options...
laffin Posted November 25, 2010 Share Posted November 25, 2010 wont keep it from going into negatives tho, just a thought UPDATE table SET qty=qty-1 WHERE name=vouchercode AND qty>0 That should fix it Quote Link to comment https://forums.phpfreaks.com/topic/219813-reducing-sql-value-by-one/#findComment-1139525 Share on other sites More sharing options...
petenaylor Posted November 25, 2010 Author Share Posted November 25, 2010 That's great any thanks! Quote Link to comment https://forums.phpfreaks.com/topic/219813-reducing-sql-value-by-one/#findComment-1139585 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.