Insert values from an array into the database

how to save data from an array in the database…

1 Like

Have a look here for a detailed breakdown of doing just that.

Well maybe explain more detailed what exactly would you like to insert where in what format.

hello Teodor, it’s been two weeks since I’ve been breaking my head with this, in fact I want to save this data from this array in a subtable as shown in the images

my subtable
Screenshot_1

my array
Screenshot_2

my server action

I don’t know how to pass these values ​​to my subtable.

I’m very grateful if you can help me.

thanks.

I don’t really understand where your data comes from exactly, but check the docs explaining how to insert data in the main and subtable:

this data and a textarea that is inside a formScreenshot_4

Screenshot_5

There is something weird in your logic. Why would you add json code in a textarea in a form? What is the idea? Probably your whole logic is wrong.

I’ll send you a video

the products are added to a datastore that feeds the textarea, I used this textarea to try to save in the subtable.

did you understand ?

You should not send all the data, especially prices from your data store to the database, as it can easily be changed in the dev tools. All you need to send to your server action is the product ID and product quantity. The rest can be calculated on the server side and inserted in the required tables.

Do you have a tutorial showing how to do this?

You need a repeat in your form and add 2 hidden inputs there one for product id and one for the quantity.
Example:

<form ...>

<div is="dmx-repeat" id="repeatItemsCart" dmx-bind:repeat="dsCart.data.reverse()">
    <input id="product_id" name="product_id" dmx-bind:name="repeatItemsCart[{{$index}}][product_id]" type="hidden" dmx-bind:value="prd_id">
    <input id="product_qty" name="product_qty" dmx-bind:name="repeatItemsCart[{{$index}}][product_qty]" type="hidden" dmx-bind:value="quantity">
...
other info here
...
</div>
</form>

Then in your server action you have these available as:

so you can do whatever you like with them - filter a query, get product price, details etc. then insert data wherever is needed.
But this is basically what you do with the form repeat component - so follow the tutorial Inserting Data in Main and Sub Table using App Connect Form Repeat

thank you very much friend this tip will help me a lot :handshake:

I don’t know why it’s repeating the first id and qty

Screenshot_1

Screenshot_2

Screenshot_3

use $index instead of 0 in value
image

OR just $id and vQTDE would be enough (without datastore1.data[$index])

That’s the correct way, you just picked the data from the wrong place.

1 Like

:stuck_out_tongue_winking_eye: :stuck_out_tongue_winking_eye: :clap: :clap:

success.!

Screenshot_4

thank you so much Teodor and famousmag

2 Likes