TeroYukio Posted August 13, 2012 Share Posted August 13, 2012 Here is my code: <?php include('header.php'); $sql = ("SELECT * FROM users ORDER BY id") or die(mysql_error()); $res = mysql_query($sql); while($rows = mysql_fetch_array($res)){ $id = $rows['id']; $sql2 = ("UPDATE users SET gold = (gold * 5) WHERE id = '$id'") or die (mysql_error()); $res2 = mysql_query($sql); echo "User: $rows[un] now has $rows[gold] gold."; echo "<br>"; } mysql_close(); ?> This is what it displays every time I refresh the page: User: asdfasdf now has 25 gold. User: terukio now has 30 gold. User: bobby now has 0 gold. When I refresh it should updated the first user to 125 gold and the second to 150 gold. However it does not do this and it does not display an error. EDIT: Even if I remove the condition WHERE id = '$id' it still gives me the same result. Quote Link to comment https://forums.phpfreaks.com/topic/267002-sql-update-function-not-working/ Share on other sites More sharing options...
Barand Posted August 13, 2012 Share Posted August 13, 2012 Note $sql variables $sql2 = ("UPDATE users SET gold = (gold * 5) WHERE id = '$id'") or die (mysql_error()); $res2 = mysql_query($sql); You can replace the whole of your posted code with a single UPDATE without a WHERE clause to update every row in the table. Quote Link to comment https://forums.phpfreaks.com/topic/267002-sql-update-function-not-working/#findComment-1368914 Share on other sites More sharing options...
TeroYukio Posted August 13, 2012 Author Share Posted August 13, 2012 *facepalm* I think its time for bed, 2:30 am I can't believe I missed that. Sorry! Quote Link to comment https://forums.phpfreaks.com/topic/267002-sql-update-function-not-working/#findComment-1368915 Share on other sites More sharing options...
requinix Posted August 13, 2012 Share Posted August 13, 2012 You can replace the whole of your posted code with a single UPDATE without a WHERE clause to update every row in the table. UPDATE users SET gold = gold * 5 Quote Link to comment https://forums.phpfreaks.com/topic/267002-sql-update-function-not-working/#findComment-1368916 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.