Jump to content


Photo

Set to -1000


  • Please log in to reply
2 replies to this topic

#1 bongo13

bongo13
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 09 April 2006 - 02:32 AM

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.


#2 IceHawk

IceHawk
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 09 April 2006 - 02:58 AM

May I recomend the below method instead;

$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);

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.
[a href="http://www.devtext.com" target="_blank"]DevText a community for all languages[/a]

#3 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 09 April 2006 - 08:14 AM

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 = user_points - 1000 WHERE 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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users