Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/267002-sql-update-function-not-working/
Share on other sites

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.