Jump to content


Photo

Problems with updating value


  • Please log in to reply
4 replies to this topic

#1 OriginalSunny

OriginalSunny
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 25 April 2006 - 09:06 PM

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.


#2 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 25 April 2006 - 09:23 PM

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 suggestions

The first im not sure will work but change this line:

$result1 = $result - $row1['quantity'];

to this:

$result1 = $result[0] - $row1['quantity'];

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 Stock
WHERE 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" WHERE
stockID = $row1['stockID'] ;
}[/quote]
If you're not part of the solution, you're part of the precipitate

#3 OriginalSunny

OriginalSunny
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 25 April 2006 - 09:54 PM

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>";

$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'];

...
}
echo '</table>';


#4 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 25 April 2006 - 10:15 PM

How about this

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';
...
}

If you're not part of the solution, you're part of the precipitate

#5 OriginalSunny

OriginalSunny
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 25 April 2006 - 11:04 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users