Solved

Best way to store values from a checkboxgroup

Posted on 2008-10-21
5
182 Views
Last Modified: 2013-12-24
Hello Experts.
I have one Insertpage for cars.I'm using checkboxes for the extras and i want to know how i can store the values in the DB.
I have tried to use :
<input type="checkbox" name="Extras" value="740" />
<input type="checkbox" name="Extras" value="750" />
<input type="checkbox" name="Extras" value="790"/>
And in the query i have :
<cfset art_uuid = createuuid()>
<cfquery name="InsertArtikel" datasource="#dsn#">
INSERT INTO ARTIKEL
(uuid,ART_ID,EXTRAS,TYP_ID,USER_ID,PRICE,KM)
VALUES
('#art_uuid#','#FORM.EXTRAS#',#Form.TYP_ID#,#SESSION.USER_ID#,#Form.Price#,#Form.KM)
</cfquery>

The result is that i have in the EXTRAS column (artikel table) if all the checkboxes are checked 740,750,790.
The question is that i have been told that this is wrong.The best way is to store the EXTRAS in a second table (extras) like:
Extras_Table_id   Art_ID    Extras
1                             5           740
2                             5           750
3                             5           790

Where the Art_ID is the  autogenerated primary id in the artikel table.(in this case 5)
If that is the right way please help me how the Insert query must be.
 
0
Comment
Question by:Panos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
gdemaria earned 500 total points
ID: 22769406
Yes, I agree that the EXTRAS table is the right approach.

To insert into it, just loop through your EXTRAS values ...

To keep from adding duplicates, remove all the extras associated with that Art_ID first
   <cfquery name="clearExtra" ....>
     delete extras where art_id = #val(art_id)#
  </cfquery>

Now add each checked extra...
<cfloop index="kk" list="#form.extras#">
   <cfquery name="addExtra" ....>
     insert into extras (art_id, extras)
     values (#art_id#, #kk#)
  </cfquery>
</cfloop>

0
 
LVL 2

Author Comment

by:Panos
ID: 22769940
Hi gdemaria.
I need the query in the same page .So i need the art_Id.
Is this the right way?
<cfset art_uuid = createuuid()>
<cfquery name="InsertArtikel" datasource="#dsn#">
INSERT INTO ARTIKEL
(uuid,ART_ID,TYP_ID,USER_ID,PRICE,KM)
VALUES
('#art_uuid#',#Form.TYP_ID#,#SESSION.USER_ID#,#Form.Price#,#Form.KM)
</cfquery>
<cfquery name="getNewID" datasource="#dsn#">
SELECT Art_ID FROM artikel WHERE uuid = '#art_uuid#'
</cfquery>
<cfloop index="kk" list="#form.extras#">
   <cfquery name="addExtra" ....>
     insert into extras (art_id, extras)
     values (#art_id#, #kk#)
  </cfquery>
</cfloop>
0
 
LVL 2

Author Comment

by:Panos
ID: 22770179
That's it:
<cfset art_uuid = createuuid()>
<cfquery name="InsertArtikel" datasource="#dsn#">
INSERT INTO ARTIKEL
(uuid,ART_ID,TYP_ID,USER_ID,PRICE,KM)
VALUES
('#art_uuid#',#Form.TYP_ID#,#SESSION.USER_ID#,#Form.Price#,#Form.KM)
</cfquery>
<cfquery name="getNewID" datasource="#dsn#">
SELECT Art_ID FROM artikel WHERE uuid = '#art_uuid#'
</cfquery>
<cfloop index="kk" list="#form.extras#">
   <cfquery name="addExtra" ....>
     insert into extras (art_id, extras)
     values (#getNewID.art_id#, #kk#)
  </cfquery>
</cfloop>
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 22770181
Right Idea,  just some small tweaks.

Looks like you're using MS SQL Server, right?   Try  SCOPE_IDENTITY() to get the ID
And you have to assign it to the variable used in the last Insert


<cfquery name="InsertArtikel" datasource="#dsn#">
  INSERT INTO ARTIKEL (uuid,ART_ID,TYP_ID,USER_ID,PRICE,KM) VALUES
  ('#art_uuid#',#Form.TYP_ID#,#SESSION.USER_ID#,#Form.Price#,#Form.KM)
</cfquery>
 
-- this fetches the last ID created in the session (more reliable!)
<cfquery name="getNewID" datasource="#dsn#">
  select SCOPE_IDENTITY( ) as ID
</cfquery>
<cfset art_id = getNewID.ID>  --- put the ID into the variable used below
 
<cfloop index="kk" list="#form.extras#">
   <cfquery name="addExtra" ....>
     insert into extras (art_id, extras)
     values (#art_id#, #kk#)
  </cfquery>
</cfloop>

Open in new window

0
 
LVL 2

Author Closing Comment

by:Panos
ID: 31508399
thank you very much for your help.
regards
Panos
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

624 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