Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

Best way to store values from a checkboxgroup

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
Panos
Asked:
Panos
  • 3
  • 2
1 Solution
 
gdemariaCommented:
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
 
PanosAuthor Commented:
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
 
PanosAuthor Commented:
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
 
gdemariaCommented:
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
 
PanosAuthor Commented:
thank you very much for your help.
regards
Panos
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now