Your update function is missing a WHERE clause. Meaning that for every row in the loop, it calculates the distance and then updates all rows to the same value. You need to add a WHERE clause that uses something like an ID column when updating the table so it will update just the one row and not all rows.
Example:
mysql_query("UPDATE field_data_field_adres SET dystans = $km WHERE id={$r['id']}");