0

In my code, I loop through 3 random users, and I am trying to add 500 to their total number of credits. The variable $credits isn't being captured correctly, as it shows blank when I try to echo it. The odd part however, is $userid (primary key) is captured into the variable. What do I need to modify in my code to fix this issue?
Or, perhaps is there a way to just add 500 to the current total of credits by using the mysql update?

<?php

include_once('../inc/connect.php');

$date = date("Y-m-d");

$drawquery = mysql_query("SELECT id FROM userstats WHERE `todaysurfed`>=250 AND DATEDIFF(NOW(),`wondaily`) > 30 OR `wondaily`='0000-00-00' ORDER BY rand() LIMIT 3");

while($row = mysql_fetch_array($drawquery))
  {

$userid = $row['id'];
$credits = $row['credits'];
$addcredits = 500;
$newcredits = $credits + $addcredits;

echo $userid."<br />";

$updatewinners = mysql_query("UPDATE userstats SET wondaily='$date', credits='$newcredits' WHERE id='$userid'");

}

?>

Thanks

4
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by karuppasamy
0

Because your select query is not returning credits field.
Change $drawquery as below.

$drawquery = mysql_query("SELECT id, credits FROM userstats WHERE `todaysurfed`>=250 AND DATEDIFF(NOW(),`wondaily`) > 30 OR `wondaily`='0000-00-00' ORDER BY rand() LIMIT 3");
0

Can't you just do this ?

UPDATE userstats 
SET wondaily=NOW(), credits=credits+500
WHERE `todaysurfed`>=250 
AND DATEDIFF(NOW(),`wondaily`) > 30 
OR `wondaily`='0000-00-00' 
ORDER BY rand() LIMIT 3
0
<?php

include_once('../inc/connect.php');

$date = date("Y-m-d"); // not neccessary because we use NOW() to get current dte in mysql

$drawquery = mysql_query("SELECT id,credits FROM userstats WHERE `todaysurfed`>=250 AND DATEDIFF(NOW(),`wondaily`) > 30 OR `wondaily`='0000-00-00' ORDER BY rand() LIMIT 3");

while($row = mysql_fetch_array($drawquery))
  {

$userid = $row['id'];
$credits = $row['credits'];
$addcredits = 500;
$newcredits = $credits + $addcredits;
echo $userid."<br />";

$updatewinners = mysql_query("UPDATE userstats SET wondaily=NOW(), credits='$newcredits' WHERE id=$userid ") or die(mysql_error());

}

?>

I think this will help you

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.