What you need to do is to update the “attachment” post and set “post_parent” to the post you want to attach it to.
$attachment = array(
'ID' => $attachment_id, // image ID
'post_parent' => $post_id, // the post you want it attached to
);
wp_update_post($attachment );