Jump to content

sql UPDATE function not working?


TeroYukio

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.