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

Link to comment
Share on other sites

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 :)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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