I am learning PHP by building a simple real estate management system. I have a form where people can add details of a house and it is added to a table called "properties". I then wanted the ability to add multiple images of the house which will then be shown as a gallery when the house is viewed.
I made a separate table to handle the image paths called "propertyimages". I can link the rows in this table with the property through the ID from the "properties" table. But my question is how can I save the image-paths with the id of the property if the property itself hasn't been saved to the table yet?
In other words, it would be easier if the property is first saved, then adding images since I have the property ID to then save the image paths, but I only have one form so how can I do this at the same time?
This is but one of many approaches to this.
At the top of your add.php
file (or whatever you have named it) generate a UUID, assign it to a variable, and within your form have a <input type="hidden" name="id" value="<?=$UUID;?">
that is passed to your process.php
file (or whatever you have named it).
As I mentioned other ways of doing it, but if you are just learning php as you indicated, this is probably one of the easier ways to handle this.
You could store the image in a temporary place and put the name in a hidden input on your house form and when the house is created you could move the picture to the right place and store the connection using the value from the hidden input. Remember to give the image a unique name on the server that you send back after it is saved and put it into the hidden input.
The best is of course to require the house to first be stored, for instance by first requiring a name for the house so you can save it
There is no relation between one form and store data in php.
If you post form after that You will get all form data in php side $_POST
so you have to add property data first and after that save image and make entry in database with property id.