Hellz Posted May 18, 2009 Share Posted May 18, 2009 hi, I want to update a selected fields in a database, the information i want to update comes from a form but i want it to decrease the value. What i mean is: The user inputs how many of a product they want and i want the stock field in the database to decrease by that many, any help? This is the current code im using but it increase the value not decrease. PHP Code: $quantity = $_POST['quantity']; $sql = "Update item (Stock) VALUES ('$quantity')"; $result = mysql_query($sql) or die ( mysql_error() );?> Quote Link to comment https://forums.phpfreaks.com/topic/158563-decrease-values-in-database-from-form/ Share on other sites More sharing options...
kickstart Posted May 18, 2009 Share Posted May 18, 2009 Hi Assuming you want to update one row, the table is Item and the unique key is ItemId, and that you have validated the quantity already. $sql = "Update item SET Stock = STOCK - $quantity WHERE ItemId = $ItemId"; All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/158563-decrease-values-in-database-from-form/#findComment-836294 Share on other sites More sharing options...
Hellz Posted May 18, 2009 Author Share Posted May 18, 2009 I get this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ProductId =' at line 1 $quantity = $_POST['quantity']; $sql = "Update item SET Stock = STOCK - $quantity WHERE ProductId = $ProductId "; $result = mysql_query($sql) or die ( mysql_error() );?> thats the code im using. ProductId is the key field in the database table. is that right? Quote Link to comment https://forums.phpfreaks.com/topic/158563-decrease-values-in-database-from-form/#findComment-836335 Share on other sites More sharing options...
kickstart Posted May 18, 2009 Share Posted May 18, 2009 Hi Looks right, assuming you have assigned a valid numeric value to $ProductId, that the ProductId column is a numeric field and that $quantity is a valid numeric value. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/158563-decrease-values-in-database-from-form/#findComment-836344 Share on other sites More sharing options...
Hellz Posted May 18, 2009 Author Share Posted May 18, 2009 yes there both numbers ? just keep gettin the same error Quote Link to comment https://forums.phpfreaks.com/topic/158563-decrease-values-in-database-from-form/#findComment-836348 Share on other sites More sharing options...
kickstart Posted May 18, 2009 Share Posted May 18, 2009 Hi Echo out $sql just before you execute the query. Lets see what it says there. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/158563-decrease-values-in-database-from-form/#findComment-836362 Share on other sites More sharing options...
fenway Posted May 19, 2009 Share Posted May 19, 2009 Also, if you're not using transactions, you statement isn't thread safe. Quote Link to comment https://forums.phpfreaks.com/topic/158563-decrease-values-in-database-from-form/#findComment-837346 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.