OriginalSunny Posted April 25, 2006 Share Posted April 25, 2006 Hi i am trying to update the value for my stock when this page is run. The value of the stock should be deducted by the quantityAs you can see the value of stockID is output and this is output correctly so why doesnt the rest of it work?? And am i better of using REPLACE INTO??? while($row1 = mysql_fetch_array($result1)) { echo "<tr><td><b>"; echo ''.$row1['stockID'].''; echo "</b></td>"; echo "<td><b>"; echo ''.$row1['quantity'].''; echo "</b></td>"; $sql = "SELECT stockAmt FROM Stock WHERE stockID = $row1['stockID']"; $result = mysql_query($sql,$connect) or die("sql: ".mysql_error($connect)); $result1 = $result - $row1['quantity']; echo "$result1"; UPDATE Stock set stockAmt = "result1" WHERE stockID = $row1['stockID'] ;}Its the bit in bold thats causing the problem. I am just using echo to test if the output for the value i want to replace it with is ok. Quote Link to comment Share on other sites More sharing options...
sanfly Posted April 25, 2006 Share Posted April 25, 2006 Im assuming you havent finished writing your update query because the rest of the code isnt working?I think the problem may be that your trying to do maths on an array. I have a couple of suggestionsThe first im not sure will work but change this line:[code] $result1 = $result - $row1['quantity'];[/code]to this:[code] $result1 = $result[0] - $row1['quantity'];[/code]The second and most likely answer is to just add an extra line[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]while($row1 = mysql_fetch_array($result1)){echo "<tr><td><b>";echo ''.$row1['stockID'].'';echo "</b></td>";echo "<td><b>";echo ''.$row1['quantity'].'';echo "</b></td>";$sql = "SELECT stockAmt FROM StockWHERE stockID = $row1['stockID']";$result = mysql_query($sql,$connect)or die("sql: ".mysql_error($connect));[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$row = mysql_fetch_array($result);[!--colorc--][/span][!--/colorc--]$result1 = [!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$row['stockAmt'][!--colorc--][/span][!--/colorc--] - $row1['quantity'];echo "$result1";UPDATE Stock set stockAmt = "result1" WHEREstockID = $row1['stockID'] ;}[/quote] Quote Link to comment Share on other sites More sharing options...
OriginalSunny Posted April 25, 2006 Author Share Posted April 25, 2006 Tried it but it keeps comming up with a blank page. Here it is again. Can anyone spot any errors causing it to come up with a blank page? Or find out why it isnt working? The bit in bold is causing the problem. Thanks.while($row1 = mysql_fetch_array($result1)) { echo "<tr><td><b>"; echo ''.$row1['stockID'].''; echo "</b></td>"; echo "<td><b>"; echo ''.$row1['quantity'].''; echo "</b></td>";[b] $sql = "SELECT stockAmt FROM Stock WHERE stockID = $row1['stockID']"; $result = mysql_query($sql,$connect) or die("sql: ".mysql_error($connect)); $row2 = mysql_fetch_array($result); $result1 = $row['stockAmt'] - $row1['quantity']; UPDATE Stock set stockAmt = $result1 WHERE stockID = $row1['stockID'];[/b]...}echo '</table>'; Quote Link to comment Share on other sites More sharing options...
sanfly Posted April 25, 2006 Share Posted April 25, 2006 How about this[code]while($row1 = mysql_fetch_array($result1)){$stockID = $row1['stockID'];$quantity = $row1['quantity'];echo "<tr><td><b>";echo "$stockID";echo "</b></td>";echo "<td><b>";echo "$quantity";echo "</b></td>";$sql = "SELECT stockAmt FROM Stock WHERE stockID = '$stockID'";$result = mysql_query($sql,$connect) or die("sql: ".mysql_error($connect));$row2 = mysql_fetch_array($result);$stockAmt = $row2['stockAmt'];echo "$stockAmt (stock amount)<br>";$newStock = $stockAmt - $quantity;echo "$newStock (new stock amount)";//$q = "UPDATE Stock set stockAmt = '$newStock' WHERE stockID = '$stockID';...}[/code] Quote Link to comment Share on other sites More sharing options...
OriginalSunny Posted April 25, 2006 Author Share Posted April 25, 2006 The correct values are now being output but as soon as i use update it come up with a blank white screen again. $q = UPDATE Stock SET stockAmt = '$newStock' WHERE stockID = '$stockID'; $q_res = mysql_query($q,$connect) or die("sql: ".mysql_error($connect)); I cant see where i am going wrong here. Quote Link to comment 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.