xwishmasterx Posted May 5, 2012 Share Posted May 5, 2012 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 ) ... 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. Quote Link to comment https://forums.phpfreaks.com/topic/262116-need-help-with-update-using-while-loop/ Share on other sites More sharing options...
floridaflatlander Posted May 5, 2012 Share Posted May 5, 2012 Place an echo for $totalshares outside your loop and see what or if $totalshares has value. Quote Link to comment https://forums.phpfreaks.com/topic/262116-need-help-with-update-using-while-loop/#findComment-1343277 Share on other sites More sharing options...
xwishmasterx Posted May 5, 2012 Author Share Posted May 5, 2012 It does have a value, as I can see the correct value displayed for each row using echo. Since the $totalshares are calculated inside the while loop, why would it have a value outside of it? Quote Link to comment https://forums.phpfreaks.com/topic/262116-need-help-with-update-using-while-loop/#findComment-1343281 Share on other sites More sharing options...
floridaflatlander Posted May 5, 2012 Share Posted May 5, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262116-need-help-with-update-using-while-loop/#findComment-1343282 Share on other sites More sharing options...
xwishmasterx Posted May 5, 2012 Author Share Posted May 5, 2012 Thanks for the reply, but found the error (very stupid one ) 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 Quote Link to comment https://forums.phpfreaks.com/topic/262116-need-help-with-update-using-while-loop/#findComment-1343283 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.