Adding multiple records to a database via a form

Hi there! I'm very new to Retool and I'm looking for some advice on adding multiple records to a database from a form with a multi-select element. I apologise if this is obvious, but I couldn't find it in the guides or on the forum.

This is an example of the form I want to use:

image

These are the database tables which feed into the form:

MainID  |  Main item        SubID  |  Sub item
----------------------      --------------------
     1  |  Main item 1          1  |  Sub item 1
                                2  |  Sub item 2
                                3  |  Sub item 3

When I click the Submit button, I want the information to be inserted into an existing table, as in the below example. ('ID' is the primary key and auto-increments.)

ID  |  MainID  |  SubID
---------------------------------
15  |  1       |  1
16  |  1       |  2
17  |  1       |  3

What's the best way of achieving this? I've been able to manage submissions when it's just one entry, but with multiple, I'm a bit stuck. I've looked into the 'bulk' options but am struggling to figure it out by myself. I'd be really grateful of any help or advice. If you need any more details, please let me know.

You would need to either map the values for the sub items or iterate over them and run an insert statement for each sub item containing the main item id:
Check out additionalScope: Run JavaScript