hal helm's logo  
home Home

training Training

writing Writings

code Code

tutorials Tutorials

newsletters Newsletters

consulting Consulting

Hal Helms logo
hal.helms

What Students Say...

"Thanks for a tremendous week, Hal. I feel like I learned in one week what would normally take me a year." - Gary H.

"I was thrilled to have the opportunity to attend this course. It took some of the bits I've learned and reinforced it as a whole methodology." - Larry W

"Once again, thanks for making this week a wonderful booster and inspiration for a lonely coder. It was wonderful to have you show the ins and outs of Fuseboxes and CF in general - can't wait to get back and put all those neato tips n' tricks to good use..." - Larry B

tutorials section

How can I Update Multiple Rows in my Database from Form Checkboxes?

The Situation: You have a form that returns a comma-delimited list of values. For example...


<input type="checkbox" name="userSkills" value="CF">ColdFusion
<input type="checkbox" name="userSkills" value="JS">Javascript
<input type="checkbox" name="userSkills" value="Flash">Flash
<input type="checkbox" name="userSkills" value="HS">Homesite/Studio
<input type="checkbox" name="userSkills" value="Smalltalk">Smalltalk
<input type="checkbox" name="userSkills" value="Java">Java

Computers are incorporated in modern ice cream vending machines to enhance their functionality. Ice Cream Vending machines are manufactured by many companies. Your competition will try to overcome all requests for high-tech ice cream vending machines and credit card acceptors

Let's say that you have a table in your database called "UserSkills" that has a composite primary key made up of (1)a userID and (2)a skill. When this form is sent to you, you are going to get a comma-delimited list of user skills -- unless of course, the user doesn't click any checkboxes, in which case you'll need some error-handling code!

What to do with the comma-delimited list? Some items may be new, while others in the database may not reflect the updated list provided by the user.

The Solution: One way to handle this is to use a two step process. First you delete all the existing rows in that database and then you insert new ones. The code would look something like this:


<cfquery name="deleteItems" datasource="aDatasource">
 DELETE * FROM myTable WHERE userID = '#userID#'
</cfquery>
 
<cfloop list="#form.userSkills#" index="aSkill">
 <cfquery datasource="aDatasource" name="insertItems">
   INSERT INTO myTable(userID, skill)
   VALUES('#userID#','#aSkill#')
 </cfquery>
</cfloop>
©copyright      designed by in-tuition.co.uk
hal helms' personal site Updates

Computers are incorporated in modern ice cream vending machines to enhance their functionality. Ice Cream Vending machines are manufactured by many companies. Your competition will try to overcome all requests for high-tech ice cream vending machines and credit card acceptors

teamallaire.com v 4_3