0

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?

  • 1
    show what you have done so far – urfusion Nov 17 '16 at 06:47
  • I am not asking for code, just a suggestion or approach. My code would be irrelevant to my question. –  Nov 17 '16 at 06:48
  • 1
    Images form part of the property details and thus you should save property and images simultaneously i.e. save property, get id, save images against id. All these actions in one transaction so property is either saved with images or it is not saved at all. – The Shooter Nov 17 '16 at 06:50
  • u have to save property first so u can get id for pictures – Asad Nov 17 '16 at 06:50
  • @TheShooter What would the search criteria be to get the ID once I save the property so that I can then add the property images? –  Nov 17 '16 at 06:53
  • @The Shooter: What if there is any issue with the uploading images and failed to save them? Your approach needs more brainstorming... – d.coder Nov 17 '16 at 06:53

3 Answers3

0

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

rypskar
  • 2,012
  • 13
  • 13
0

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.

Foramkumar Patel
  • 299
  • 3
  • 10
0

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 as you indicated, this is probably one of the easier ways to handle this.

Community
  • 1
  • 1
Abela
  • 1,225
  • 3
  • 19
  • 42
  • That is an elegant solution! I am definitely going to incorporate this into my code :) Thanks –  Nov 17 '16 at 06:58