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. Quote 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)" Quote 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 Quote Link to comment https://forums.phpfreaks.com/topic/238577-restock/#findComment-1226503 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.