Solved

Multiple Select Entry into DB

Posted on 2001-06-17
7
128 Views
Last Modified: 2013-12-24
I have a good question.  I have a multiple Select box that the user chooses one or more item from.  The items selected are then to be inserted into a DB table.  Currently it is working, although not the way I want.  Now, when I enter the select into the DB, it enters ONE record such as "Item1, Item2, Item 3, etc."  I would like EACH Item to be entered into the DB as it's own record.  Has any one done this?  I know it may involve parsing and Looping, but I'm too new to Coldfusion to know exactly.

Any Ideas?
0
Comment
Question by:khagen
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Expert Comment

by:CF_Spike
Comment Utility
When someone selects multiple items from a select box the values are sent as a comma separated list. If you want to loop over this list and enter the records into your database you can do it quite easily as follows:

<CFLOOP list="#form.selectfield#" index="i">
<CFQUERY DATASOURCE="dsn">
INSERT INTO mytable
(column1,column2,column3)
VALUES
('#form.field1#','#form.field2#','#Evaluate('form.' & i)#')
</CFQUERY>

This assumes that the form fields are all text fields in the database and that you want to put the same data for fields1 and 2 into the database for each value that was selected in the select box.

If you want to only insert the values from the select box you can do the following:

<CFLOOP list="#form.selectfield#" index="i">
<CFQUERY DATASOURCE="dsn">
INSERT INTO mytable
(column)
VALUES
('#Evaluate('form.' & i)#')
</CFQUERY>

You need to replace the names of the form fields, datasource and columns in the database from these examples, but otherwise it should work.

Spike
0
 
LVL 1

Author Comment

by:khagen
Comment Utility
Thanks CF Spike,
But I am Getting an Error,

Here is my Code,
<CFLOOP list="#form.SkillSet#" index="i">
          <CFQUERY DATASOURCE="DataSource">
          Insert into SkillSet(Skill, StaffingID)
          Values('#Evaluate('form.' & i)#', '#Session.StaffingID#')
          </CFQUERY>
          </CFLOOP>

The Error is;

An error occurred while evaluating the expression:
#Evaluate('form.SkillSet' & i)#

An error has occurred while processing the expression:

   form.SkillSet10 KEY BY TOUCH


Invalid parser construct found on line 1 at position 17. ColdFusion was looking at the following text:

KEY


0
 
LVL 1

Author Comment

by:khagen
Comment Utility
form.SkillSet10 KEY BY TOUCH

SkillSet is the name of the Select, and "10 KEY BY TOUCH" is the first of several selected items.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 5

Accepted Solution

by:
Yog earned 215 total points
Comment Utility
try this

<CFLOOP list="#form.SkillSet#" index="i">
 <CFQUERY DATASOURCE="DataSource">
 Insert into SkillSet(Skill, StaffingID)
 Values('#i#', '#Session.StaffingID#')
 </CFQUERY>
</CFLOOP>

What spike has said is right , but since we looped , we can just specify the index value here

Cheers.
0
 
LVL 1

Author Comment

by:khagen
Comment Utility
Thank you Yog,
Cf Spike, I wish I could split my points, but, Yog did get me over the Hump.  Thank you both!
0
 
LVL 4

Expert Comment

by:CF_Spike
Comment Utility
You're welcome.

For future reference. If you want to split the points you can reduce the points for the current question by half and post a second question saying something like "Points for CF_Spike" then award the points as an answer for that question

Spike
0
 
LVL 5

Expert Comment

by:Yog
Comment Utility
ur welcome..

or now i guess just post a comment in the Community support topic and ask to split the points, include the question number also..

cheers.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now