Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Inserting multiple checkbox values into db

New Here ,
Feb 12, 2007 Feb 12, 2007
i am struggling to insert multiple checkbox values into a database table using one update statement. i guess that i will need to create and array to hold the values and then insert them into the db table. can anyone help with sample code?
TOPICS
Database access
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 12, 2007 Feb 12, 2007
Hi,

No need to create arrays and all.. Even though your check box holds various values if you name them uniquely it will inturn will get saved as a comma delimited values in DB...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 12, 2007 Feb 12, 2007
quote:

Originally posted by: Daverms
Hi,

No need to create arrays and all.. Even though your check box holds various values if you name them uniquely it will inturn will get saved as a comma delimited values in DB...



Actually, you have to give all your checkboxes the same name to get that list. Storing the list as in a single record is not necessarily the best idea.

For the original poster. Are you trying to insert new records or update existing ones?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 12, 2007 Feb 12, 2007
You didn't say what DB - here's what I do for MSSQL.

Name the checkboxes the same as the fields you want to insert.

If this is actually an update to an existing record, you will need to include any key fields as hidden fields in the form.

Either way, simply use either:

<cfinsert datasource="yourdatasourcename" table="yourtablename" formfields="field1,field2,field3">

OR (for an update):

<cfupdate datasource="yourdatasourcename" table="yourtablename" formfields="primarykeyfield,field1,field2,field3">

Hope this helps.

- Mike
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 12, 2007 Feb 12, 2007
HI guys,

Thanks a stack for the info. I am using MySQL v 5 and trying to insert and later update info to a table. For example I have employees table, hobbies table and emp_hobbies table. Each employee can select a number of hobbies taken from hobbies table and populated into an array and displayed as checkboxes.

Once they select the checkboxes it must insert (and later update) the emp_hobbies table by adding a row for every employee and their hobbies selected. Each cjheckbox has its own hobby_id from the hobbies table and this is the id that needs to be inserted into emp_hobbies with the employees unique id (emp_id).
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 13, 2007 Feb 13, 2007
For the first part, I believe that mySql supports this kind of syntax:

insert into mytable
(field1, field2)
values
((value11, value12),(value21, value22)) etc.

To populate the emp_hobbies table, it might be simpler to delete all the existing rows and re-insert new ones.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 19, 2007 Feb 19, 2007
Thanks Dan - got the syntax sorted.

However, is there a way to insert multiple rows into a table in one insert statement so that for each hobby that an employee chooses he/she has a seperate row with their emp_id and then also the hobby_id?

If an employee chooses 3 hobbies the emp_hobby table must contain 3 rows for that employee.

All the checkboxes have the same name - hobby - in nthe form.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 19, 2007 Feb 19, 2007
Since you have the syntax down, the next step is to realize that form.hobby will either not exist at all, or it will be a list. The cfloop pages in the cfml reference manual have a section on looping through a list. If you don't have one, the internet does.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 20, 2007 Feb 20, 2007
LATEST
Thanks Dan - appreciate your help!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources