How To Connect Id Before Saving Post?

How to connect an image before saving a post?

Like this forum. when you post you can add an attachment. The attachment is uploaded, but how is the post_id filled?

I can take the next id of the table, but if someone else post before I submit, the id is wrong.

I only can thing of a temporary id in a session and a temp_id in the table. But it seems like an ugly solution.

Save the post first, then you can check its ID attribute to find out which ID was assigned.

Could be a solution. Then I have to add something like a ‘in progress’ bool to the table i guess.

Why? You can add the image in the same request.


I work with an editor with a filebrowser option. You can upload images, and select them in your post.The images has to be saved (make thumbnails, watermark etc.) and connected to the post for later. When I edit a post and open the filebrowser, it shows all the images with the current post_id.

So this can’t be done in the same request I guess.

edit: id did it with sessions, but it became a mess. Now I also store the user_id in my post_image table. So when creating a post, the images have post_id=0. When submitting a post, I update the post_id for all the images where post_id is 0 and the user_id is the current user.

So in the worst case scenario, a user start a post, upload images and then decides to cancel. Then he starts a new post, uploads images again and after save he also see the images he was uploading in the cancelled post. I can live with that.

I suppose you could have generated some sort of GUID on the first page load, and kept that in a hidden form field, using it to identify the images belonging to the unsaved post. You could store that in the database when the post record is saved.

I’d be averse to storing the data in sessions because it will cause problems if the user is trying to write different posts in different tabs (for instance, if they start a new post after they’ve started writing the content for another one).

Thanks Keith. I will look into that as well.