Solved

Best way to store values from a checkboxgroup

Posted on 2008-10-21
5
174 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
  • 3
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
gdemaria earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thank you very much for your help.
regards
Panos
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

16 Experts available now in Live!

Get 1:1 Help Now