It's kind of hard to tell what you're trying to do from the code you've posted, but I'm pretty sure you can't update all the data you want to update using a single SQL update statement.
Instead of repeatedly appending to $query inside the loop and calling mysql_query on the huge $query variable, call mysql_query inside the loop, updating one record each time.
There are other errors in your code too, namely using single quotes like this:
$query .= '$newvalue';
That will append the literal string $newvalue, not the value of the $newvalue variable. Use double quotes to get PHP to process the variable:
$query .= "$newvalue";
Or, because you're only appending the contents of the variable with nothing else, drop the quotes entirely:
$query .= $newvalue;