How to manually set an attachment in a post?

enter image description here

I need to manually set an attachment in a estate post with custom php.

When it was the wordpress that inserted the image into the media library, this image appears in the post on frontend.

However, when the user inserts this attachment into the database even the url of an existing image, the image does not appear in the post.

enter image description here

How can I solve this?