Jump to content

Archived

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

bongo13

Set to -1000

Recommended Posts

well, heres my script

<?
$query = "select * from users where user_username = '$username'";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
$old_points = $row['user_points'];
}

$points = $old_points - 1000;

$query = "UPDATE users SET user_points = '$points' WHERE user_username = '$user_name'";
$result = mysql_query($query);
?>

1.it sets the users points to -1000 when i want it to take away - 1000 from the original points
2. the variable $query is set before in the script, i tried putting $query1 in but it didnt make a difference

if you need any more information just post it and i'll see what i can do while you see what you can do.

Share this post


Link to post
Share on other sites
May I recomend the below method instead;

[code]
$query = "select user_points from users where user_username = '$username'";
$result = mysql_query($query);
list($old_points) = mysql_fetch_row($result);

$points = $old_points - 1000;

$query = "UPDATE users SET user_points = '$points' WHERE user_username = '$user_name'";
$result = mysql_query($query);
[/code]

Replacing the needless while loop with list($old_points) = mysql_fetch_row($result) saves some lines. And specifying in the query to only select the column you're working with will save time on the query as well.

A possible cause of your error I see is that in the first query you use $username, but in the update you use $user_name. Not sure which one holds the actual username you're using though, but you will want to double check that.

Share this post


Link to post
Share on other sites
to avoid the whole issue, reference the original value itself:

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']UPDATE[/span] users SET user_points [color=orange]=[/color] user_points [color=orange]-[/color] 1000 [color=green]WHERE[/color] blah blah blah [!--sql2--][/div][!--sql3--]

you don't have to waste resources by grabbing it in the first place. mysql already knows the original value.

Share this post


Link to post
Share on other sites

×

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.