Jump to content

Need help with UPDATE using WHILE loop.


xwishmasterx

Recommended Posts

Hello

 

Still pretty new to php, so I need a little help with the below code  (It's not pretty, but for now I just need it to work correct :P)

 

...
while($row = mysql_fetch_array($result)){
if($row['user_group_id'] <= 2){
$multiplier = 1;
}
if($row['user_group_id'] == 6){
$multiplier = 2;
}
if($row['user_group_id'] == 7){
$multiplier = 3;
}
if($row['user_group_id'] == {
$multiplier = 4;
}
$totalshares = ((($row['activity_blog']*$multiplier)/2)+(($row['activity_comment']*$multiplier)/10));

$set_shares=mysql_query("UPDATE phpfox_user SET bod_shares=$totalshares WHERE profile_page_id=0");


echo " User id: ".$row['user_id']." multiplier:".$multiplier." User group:".$row['user_group_id']." 
- blog:".$row['activity_blog']." - comment:".$row['activity_comment']." totalshares= ".$totalshares."";
echo "<br />";


}
?>

 

Problem is the UPDATE part "bod_shares=$totalshares", which just inserts "0".

How do I get it to update the row with the correct value for each row?

The echo shows the right values and calculation for $totalshares.

Link to comment
https://forums.phpfreaks.com/topic/262116-need-help-with-update-using-while-loop/
Share on other sites

I mean like test

 

...
while($row = mysql_fetch_array($result)){
if($row['user_group_id'] <= 2){
$multiplier = 1;
}
if($row['user_group_id'] == 6){
$multiplier = 2;
}
if($row['user_group_id'] == 7){
$multiplier = 3;
}
if($row['user_group_id'] == {
$multiplier = 4;
}
$totalshares = ((($row['activity_blog']*$multiplier)/2)+(($row['activity_comment']*$multiplier)/10));

$set_shares=mysql_query("UPDATE phpfox_user SET bod_shares=$totalshares WHERE profile_page_id=0");


echo " User id: ".$row['user_id']." multiplier:".$multiplier." User group:".$row['user_group_id']." 
- blog:".$row['activity_blog']." - comment:".$row['activity_comment']." totalshares= ".$totalshares."";
echo "<br />";


}
// echo for test
echo "Totalshares: $totalshares";
?>

I allways put echo "Whatever: $whatever"; so at least something prints and lets me know the echo is working.

Thanks for the reply, but found the error (very stupid one :P)

The update, updates ALL rows where profile_page_id=0, hence all rows are update with the last value. Added:

...WHERE profile_page_id=0 AND user_id= ".$row['user_id']."";...

An it updates the value correct.

 

Thanks for leading me in the right direction :)

 

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.