Cannot replicate operations of a single db insert for a multi-insert

Hi,

I’m looking to do a database insert where:

One of the fields comes from a server-side variable

  • set_session: set $_SESSION.test_var to some value
  • db insert into CARS table: the MODEL field is bound to $_SESSION.test_var

Another fields’ value comes from the button on the frontend. Depending on which submit button I click, I want a different value to be inserted for a given field (YEAR) and for each record to be inserted.

  • button 1 - dynamic event > flow: 1)set value of YEAR to 2000, 2)submit form
  • button 2 - dynamic event > flow: 1)set value of YEAR to 2010, 2)submit form

Both fields are correctly filled out when doing a single insert, but they’re blank on multi-insert. I believe the multi-insert is working properly as it submits properly when I set static or dynamic values from the properties panel. Is there something I should do differently? Thanks!

Hi,
The post seems extremely confusing as to what is on the server side and what is on the client side.

There is no such thing as multi-insert in Wappler Server Action from what I know. You need to set a repeater with insert step inside to run insert queries multiple times.

Can you please be a bit more clear on what the structure of your SA is, and what is the form HTML that you are submitting from the client side?

I was trying to use this - should I set up the repeat and insert manually instead?

There are 2 values I’m trying to insert.

  1. a server-side only variable under $_SESSION

  2. a client-side variable. Depending on which submit button I click, I want the button to set the value to a field, and then submit. The SA is directly connected to the insert API (the single insert API works perfectly, the multi-insert does not with the exact same set up)

The setup from client does not match the server action setup actually.
You need to send multiple values as array to run a repeat.
If your use case is that you need to insert 1 row on click of 1 button, and 2nd row on click of second button, you don’t need multi-insert. A regular insert step is fine.

The multi insert is helpful only when you have an array of information being sent to the SA.

On a side note: Seeing that you have joined just two days ago, in case you don’t have come from a programming background, a quick crash-course on HTML, CSS, JS, JSON & APIs would help a lot with learning Wappler… followed by @mebeingken’s tutorials (in case you haven’t done any of that).

The 2nd and 3rd screenshots are what I have for the single insert. This is what I have for the multi-insert. The Dynamic Event is the same.

Let me know if I got this wrong, but I think I do need a multi-insert as I need to insert multiple rows at the same time, with the click of 1 button.

If your use case is that you need to insert 1 row on click of 1 button, and 2nd row on click of second button, you don’t need multi-insert.

I think you misunderstood what I need from the 2 buttons. Both buttons would be “Submit” buttons for the entire form (and insert multiple records). The difference is that button 1 will set the value of the field YEAR for all rows to ‘2020’ before submitting, whereas button 2 will set the value of ‘2019’ to the YEAR field of all rows before submitting.

This looks a bit more clearer.
Since you do have a repeat setup here, multi-insert would be something that you need to do.
Ensure that all the NAME properties are set to something like year[], make[], model[] etc…
The square brackets ensure the value sent to server action is an array.
Or, you can make it a JSON kind of structure: record[year] (i think. don’t remember the exact syntax right now)
Then, in server action, you can repeat on one of the items, say year and use rest of the params as $_POST.model[$index]
Or, you can repeat on record, and set the values to be {{year}}, {{model}}, etc.

You can check in the network tab of browser dev tools to ensure the data being sent is either in array or record form.

Hi, thanks for the advice but I’m not sure I follow.

This is how the multi-insert form is set up, automatically with the form generator. If I type in the values manually into the fields of the form and click submit, all the values and records will be inserted properly.

Now I’ve set up a button that should set the value to the Model field, and then submit. Submit works (I see new records), but no values were submitted.

Given there’s no ‘Repeat’ in the second screenshot under ‘serverconnectform1’, I’m assuming Set Value is expecting an actual value not an array? Either way I’ve tried both: [“a”, “b”] as in the screenshot, and then just ‘test_value’. Neither value are inserted.


There’s also the 2nd problem. On the back-end I want a field (‘test’) to be filled with a global $_SESSION variable (session_test_var, which has already been set on app load). This binding works if I’m doing a single insert, which means that the $_SESSION variable has a value. However, with a multi-insert, the field is blank for the inserted records. I’ve tried the $_SESSION from within the repeat as well - same results.

Can you share a link to the page?
I think the "model’ field’s name needs to be record[model][] or record[model] and not just model as shown in the screenshot.
But I am not sure as I am unable to follow the HTML structure here.

The page isn’t live anywhere, but I’ve attached the code here. The button is at the very button after the default Submit button.

Looking at it it makes sense why it’s not working. The ‘setValue’ action is outside the repeat, so it can’t set value to individual records.

How would you set up a form with 2 buttons, where:

  • if you click on one button, it sets the value for one field of all records to ‘value1’ and then submit
  • if you click on button 2, it sets the value to ‘value2’ for all records and then submit?

The dumb solution I have is to set up 2 forms, hardcode a different static value for each form, and have each form have its own submit button. But this doesn’t seem like the most elegant approach…

And do you have any thoughts on problem #2? Any idea why the multi-insert API can pick up global variables such as UTC, but not $_SESSION variables that I have set before?

About the value part. You need to create a variable. Then set that variable as the dmx-bind:value for the YEAR input.
On click of the 2019/2020 submit button, just set the variable value to 2019/2020, and it will set all the YEAR inputs with that value just before submitting the form.

I do not understand your second problem. You are putting the string inp_$_SESSION in the ID of some fields. And you are also setting the name of some record field to be $_SESSION.
Both are not recommended. $_SESSION is kind-of a reserved keyword for Wappler.
I don’t understand at all why something like this would even ever be required. I would really like to know what you are trying to achieve with this.