PHP:循环内部需要求和

The student is back. Thanks for you help an patience ;)

I'm using a foreach loop like this:

foreach ($value as $val)
{
//coming from database, from a select query:
$points = $row['points'];  

//now I want to update another database table:  
 $sql = "update user set totalpoints=".$points." where userid=".$uid." ";

//but by doing this, table user is updated with the value of $points in the last iterarion 

}

What do I have to do to update my table with the summation of $points and not with the value of the last iteration?

Thanks again

Not 100% sure but it seems you want:

$sum = 0;

foreach ($value as $val) {
    $sum += $row['points'];  
}

$sql = "update user set totalpoints=".$sum." where userid=".$uid;
$points = 0;
while ($row = mysq_fetch_array()) // from where you are getting row
{
//coming from database, from a select query:
$points += $row['points'];  

}


//now I want to update another database table:  
 $sql = "update user set totalpoints=".$points." where userid=".$uid." ";

You should do it all in one query

I suppose your original query was something like SELECT * FROM matches WHERE userid='$uid' (whatever) so instead do something like UPDATE user SET totalpoints=(SELECT SUM(points) FROM matches WHERE userid='$uid' GROUP BY userid) WHERE userid='$uid' (Probably it would not fit your exact problem but I hope you get the idea)