0
How to insert multiple list item as separate records

/t5/coldfusion-discussions/how-to-insert-multiple-list-item-as-separate-records/td-p/2518
Jul 17, 2006
Jul 17, 2006
Copy link to clipboard
Copied
I have a form with a list field in which user can select
multiple item <select multiple="multiple"><option
value></option></select>. My problem is that I don't
know how to do an insert sql operation that puts every selected
item on the list field into a separate record in a database table.
I think that first I should get it to know how many item was
selected from the list, than write the insert into statement
between loop tags which loops the operation until every item has
inserted. I'm not sure about it and even if I'm right, I don't know
how to code it. Could someone show me a sample code for such a
"problem"?
TOPICS
Getting started
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Enthusiast
,
/t5/coldfusion-discussions/how-to-insert-multiple-list-item-as-separate-records/m-p/2519#M292
Jul 17, 2006
Jul 17, 2006
Copy link to clipboard
Copied
Let's assume the select list is named "mySelect"
Then
<cfloop index="listElement" list="#form.mySelect#">
<cfquery name="qry_insert" datasource="dsn">
Insert Into myTable(myColumn)
Values('#listElement#')
</cfquery>
</cfloop>
Note: This assume that the value being inserted is text.
Ken
Then
<cfloop index="listElement" list="#form.mySelect#">
<cfquery name="qry_insert" datasource="dsn">
Insert Into myTable(myColumn)
Values('#listElement#')
</cfquery>
</cfloop>
Note: This assume that the value being inserted is text.
Ken
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/coldfusion-discussions/how-to-insert-multiple-list-item-as-separate-records/m-p/2520#M293
Jul 17, 2006
Jul 17, 2006
Copy link to clipboard
Copied
The ScareCrow wrote:
> Let's assume the select list is named "mySelect"
>
> Then
>
> <cfloop index="listElement" list="#form.mySelect#">
> <cfquery name="qry_insert" datasource="dsn">
> Insert Into myTable(myColumn)
> Values('#listElement#')
> </cfquery>
> </cfloop>
>
> Note: This assume that the value being inserted is text.
uh, depending on the db (say it's sql server) you might want to swap the loop &
cfquery to make that one big insert rather than a bunch of cfqueries.
> Let's assume the select list is named "mySelect"
>
> Then
>
> <cfloop index="listElement" list="#form.mySelect#">
> <cfquery name="qry_insert" datasource="dsn">
> Insert Into myTable(myColumn)
> Values('#listElement#')
> </cfquery>
> </cfloop>
>
> Note: This assume that the value being inserted is text.
uh, depending on the db (say it's sql server) you might want to swap the loop &
cfquery to make that one big insert rather than a bunch of cfqueries.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Participant
,
/t5/coldfusion-discussions/how-to-insert-multiple-list-item-as-separate-records/m-p/2521#M294
Jul 18, 2006
Jul 18, 2006
Copy link to clipboard
Copied
PaulH, would you put some code out on what you mean. If I
want multiple DB records, how would I code it if I only have one
big insert statement?
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
LATEST
/t5/coldfusion-discussions/how-to-insert-multiple-list-item-as-separate-records/m-p/2523#M296
Jul 18, 2006
Jul 18, 2006
Copy link to clipboard
Copied
quote:
Originally posted by: Newsgroup User
The ScareCrow wrote:
uh, depending on the db (say it's sql server) you might want to swap the loop &
cfquery to make that one big insert rather than a bunch of cfqueries.
The following approach will work with just about any db and only uses once connection
<cfquery>
insert into sometable
(field1, field2, etc)
<cfloop list = "#somelist#" item = "thisitem">
select distinct #thisitem#, 'something else', etc
from some_small_table
<cfif listfind(somelist, thisitem) lt listlen(somelist)>
union
</cfif>
</cfloop>
</cfquery>
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Contributor
,
/t5/coldfusion-discussions/how-to-insert-multiple-list-item-as-separate-records/m-p/2522#M295
Jul 18, 2006
Jul 18, 2006
Copy link to clipboard
Copied
This should give you one, large, multiple insert query.
<cfquery name="qry_insert" datasource="dsn">
<cfloop index="listElement" list="#form.mySelect#">
Insert Into myTable(myColumn)
Values('#listElement#')
</cfloop>
</cfquery>
<cfquery name="qry_insert" datasource="dsn">
<cfloop index="listElement" list="#form.mySelect#">
Insert Into myTable(myColumn)
Values('#listElement#')
</cfloop>
</cfquery>
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

