0

I find it difficult to find a good title I hope I can explain it better. I have two tables

"car" - id, car_id, name
"car_pictures" - id, fk_car_id, url

Obviously a "car" can have several pictures those are linked by the car_id saved as "fk_car_id" in the "car_pictures" table. Workflow in a frontend could be:

- you add a new car to the database
- you add pictures to this car entry

If followed this way you first create a new entry in the "car" table. Then you have a "car_id" you can reference in the "car_pictures" table.

But what if you like to change the workflow like this:

- you add a new car
- you immediately add pictures
- you save the new car to the database

If followed this way you do create the database entry only if the user has finished all "steps". But if you want to add pictures to the car there is no "car_id" for it yet. So you can't reference the id.

What is the usual way to do this if adding the car and adding the pictures are two seperate api calls for example.

It seems I need to clarify what I am doing. Let's say I have two api calls: www.domain.com/api/car/add/?name=newcar -> for adding a car (ie. adding to the database) www.domain.com/api/car/picture/add/?car_id=x -> for adding a picture to a car (ie. handle the upload and put data to the database)

The frontend would look like this: A page with a table with all cars which you can edit and add new pictures to it in another page. Another page to add a new car which also has the possibility to add picture. This would be the same page/form as editing a car only that no car_id is available.

Edit:

I myself can think of several ways to handle this case.

first: create a database entry for a car immediately when the user opens the "add new car" form. that way you always have a car_id. but you have to handle if the user decides to cancel adding a car and need to remove those "dummy" database entries. if something goes wrong there you may end with a lot of empty car entries in the database. maybe the row has a flag that is set when the car is actually "inserted" by the user.

second: insert new pictures with temporary negative fk_car_id and update those rows with the actual car_id if the row is created in the database.

third: save new pictures into a temporary table with a temporary id when there is no car_id yet. then you have to return that temporary id and moved those entries from the temporary table on the creation of a car entry in the database.

But those ideas sound complicated and I hoped there is some best practice way how to do it. Maybe the problem is in my workflow itself and there is a better way to do it. Yet it seems to be a good idea to separate the process of adding a car and adding pictures to a database.

4

2 回答 2

0

添加要用于mysql_insert_id()获取在最后一个查询中生成的 ID 的汽车后。然后就可以提交图片了。

例如:

mysql_query("INSERT INTO car (name) VALUES ('car')");
$last_id = mysql_insert_id();
mysql_query("INSERT INTO car_pictures (fk_car_id, link) VALUES (".$last_id.",'link')");

我建议您使用PDO而不是mysql_

于 2013-02-27T17:11:31.340 回答
0

你可以car_pictures.fk_car_id设为空,然后你可以插入一个没有主记录的图片,然后在添加汽车时更新它......

于 2013-02-27T17:13:56.927 回答