AnAmericanGunner Posted June 6, 2011 Share Posted June 6, 2011 I am trying to create a script that first sets stock to zero (which it does), and then randomly select ten items to restock. Here is my code: <? include('includes/db.php'); if(isset($_POST['submit'])) { $updateTack = "UPDATE tack SET stock='0'"; $Tackresult = mysql_query($updateTack) or die (mysql_error()); $updateFeed = "UPDATE feed SET stock='0'"; $Feedresult = mysql_query($updateFeed) or die (mysql_error()); $i = 1; if($i < 11) { $tack = rand(1, 12); $tackquery = "SELECT * FROM tack WHERE itemid='$tack'"; $tackresult = mysql_query($tackquery) or die (mysql_error()); while($tackrow = mysql_fetch_array($tackresult)) { $sql = "UPDATE tack SET stock='10' WHERE itemid='$tack'"; if(mysql_query($sql)) { echo "<center>Successfully updated tack stock.</center>"; } else { echo'<p>Error: ' . mysql_error() . '</p>'; } $i++; } } $i = 1; if($i < 11) { $feed = rand(1, 12); $feedquery = "SELECT * FROM feed WHERE itemid='$feed'"; $feedresult = mysql_query($feedquery) or die (mysql_error()); while($feedrow = mysql_fetch_array($feedresult)) { $sql2 = "UPDATE tack SET stock='10' WHERE itemid='$feed'"; if(mysql_query($sql2)) { echo "<center>Successfully updated feed stock.</center>"; } else { echo'<p>Error: ' . mysql_error() . '</p>'; } $i++; } } } echo "<form action='test29.php' method='post'> <input type='submit' value='Submit' name='submit'> </form>"; ?> The tack table restocks two items and the feed doesn't restock any. Link to comment https://forums.phpfreaks.com/topic/238577-restock/ Share on other sites More sharing options...
btherl Posted June 7, 2011 Share Posted June 7, 2011 Try "while($i < 11)" instead of "if($i < 11)" Link to comment https://forums.phpfreaks.com/topic/238577-restock/#findComment-1226215 Share on other sites More sharing options...
AnAmericanGunner Posted June 7, 2011 Author Share Posted June 7, 2011 Thank you! It works now Link to comment https://forums.phpfreaks.com/topic/238577-restock/#findComment-1226503 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.