Dynamic data record in 2 tables

0

Good afternoon guys.

I have the following situation:

In a vehicle registration module, it is necessary to dynamically register a vehicle in a single vehicle followed by all its models and group, being that a vehicle can have N models and be registered in N groups, what is the best way for me to do it such registration

In other words, the user enters the vehicle registration module and clicks ENTER VEHICLE then opens a registration screen and within this register he informs the name of the vehicle and then on the same screen click on add model, and register the model, photo of this model and insert this model inside a certain group, then he clicks ok next to it and this data is there in stand by (not yet registered in the bd) then after this the user has the option to click again to register model and register another model with other photos and in another group and then click on ok and this data is also in stand by.

Now comes the difficult part, below this form has a register button, when I click there will register the vehicle_id, vehicle_title and vehicle_name in the VEHICLE table and the model_model, vehicle_cover, vehicle_module in the VEHICLE_MODEL table.

The question is, what is the best way for me to accomplish this? Can I simultaneously record the data of 1 SINGLE FORM in 2 TABLES IN BD? Remembering that within this FORM there may be 1 or more model array, that is, if I register a vehicle and within the register 2 more models will be recorded the vehicle data in the vehicle table and 2 model records with different ids in the modelmodel table

    
asked by anonymous 19.05.2016 / 18:23

2 answers

0

It will store in an auxiliary table when it finishes the registers ai it goes from the auxiliary table to the main one or it stores all the data in session thus inserting in the table when finishing the registers

    
19.05.2016 / 18:52
0

Herman,

In relational databases you can not insert into more than one table at a time. What you need to do is perform your inserts using a transaction after all the transaction will prevent inconsistencies in the event of an error.

Let's say you will enter the GOL car and it will have 3 models [a, b, c]. You will start a transaction- > insert the car gol - > insert templates - > close the transaction. Ah but if there is an error writing the B model? Calm, the bank will undo all changes related to that transaction in question.

BEGIN TRAN T1;
INSERT table1 ...;
SELECT table1 ...;
INSERT table2 ...;

COMMIT TRAN T1;

Font

    
19.05.2016 / 18:56