Ah. But of course: your loop reads all the results from the SELECT query and then exits. You need to move the INSERT inside the mysql_fetch_array loop.
More generally, you need to study up on the mysql functions a bit more, and look at some of the tutorials, as there's a lot more going on here than needs to:
$result=mysql_query($query);
while($row=mysql_fetch_array($result)){
...
This is where I referred to earlier that you're not checking the result of the query. Instead, this needs to look something like this:
$result=mysql_query($query);
if (! $result)
die('The programmer made a mistake: ' . mysql_error() );
while($row=mysql_fetch_array($result)){
...
Then, when you're only retrieving a single column, there's very little reason to use the associative-array version of the fetch, and absolutely no reason to fetch into an actual array--because all you do with the array is turn around and retrieve its value into a scalar variable. Instead, just use list() to put it directly where you want it:
while( list($new_code) = mysql_fetch_row($result))
{
// do your INSERT here
}