The solution is simple: in the first loop, you are setting the $amazon_link
like this:
$amazon_link = get_field('amazon_link', $image['ID']);
but in the second loop, you are setting a $link
, like this:
$link = get_field('amazon_link', $image['ID']);
but you’re still trying to echo $amazon_link
variable later on, which has never been setup in the second loop, so it’s value is still the value of the last book from the first loop.