Saving multi select field to database

Hi everyone,

Can someone help with providing a way to save a multi select field to the database.

I have three tables - doctors, location, and a many-to-many to link them. A doctor can work at multiple locations, and a location can have multiple doctors.

I am trying to add a new doctor to the doctors table using a form, I have set up the server connect to the doctors table and I am using a multi select field to choose the locations which the doctor can work from.

I am having troubles trying to insert the multiple locations from the form to the doctors table or to the many-to-many table. What is the best way to do such an action?

Thanks,

I’m just doing something similar…replace Staff with Doctors and Permissions with Locations on the screenshot below and you should find it 's what you’re after…

If you’re using the Wappler multi select or similar you’ll need to replace the split with .values (this is in the Collections drop down). Both options are covered here:

This is the server connect:

image

So create an insert for your doctor, then add a repeat taken from the multi select for each location. The insert for the location will need to include the Doctors ID and the Location ID (+ any other data you need, if any).

Hey @sbecks,

Thank you for your prompt response.

I tried replicating what you did, however I am constantly getting “Array to string conversion error” . I think it is due to the repeat step in server connect. Here is a snapshot of the server connect:

image

btw doctors are now referred to as Specialists in this example, and myArrayList is an input that contains all the values from the multi select field, I tested it and it contains the correct values.

Do you have any idea why, how I could resolve this? I tried with the .values method as well and it didn’t work, same error.

If you’re using a multi select make sure your $post is set as an array, rather than a variable in server connect, and that the name of the input on the page has at the end, so myArrayList. Then use the values method.

You’ll only need to use the split method if you’re populating a hidden text field with an array, which it doesn’t sound like you are.

Also you should be able to get the ID of the created specialist from the insert step, so may not need the query directly after? When you select the expression for the repeat have a look for identity under the insert step.

Those are opening and closing square brackets, on my phone it looks like it’s converted them to a square!

Thank you @sbecks. I managed to fix the array to string error.

Sorry to keep bothering you with questions, but I am still a newbie and have been trying to figure this out for a while.

Now I am facing another issue, the many to many table is being populated with only the first value from the array. It seems like the repeat action is not working properly, because it should populate more than once if I have selected multiple fields. Here are a few snapshots to show you what I am getting back.

image
image
image

For the locationID field, in the insertLocations action I just put the value as $value, and I have changed the myArrayList to an array and added at the end.

Just double checking:

Form field (the multi select) should have a name of myArrayList

The $POST should be set as an array (not a variable) and called myArrayList (no brackets)

The insert should be {{$value}} (which should be available through the UI pickers)

If all the above is true it should work, could you post up the multi select code, the $post variables and the location insert if not?

1 Like

Done. Got it fixed now. Thanks

1 Like