SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    adding multiple checkboxes values to database

    hi all

    i m able to echo all the checkboxes selected through the below code

    Code:
    <input  type="checkbox" value="Battery"  name="checkbox[]" id="checkbox[]" 
    />
    PHP Code:
    for ($i=0$i<count($_REQUEST['checkbox']);$i++) {
    echo 
    "chosen<br />$i. ".$_REQUEST['checkbox'][$i];

    ouput is
    Code:
    0. battery
    1. rocket
    2. pineapple
    but i want to insert all selected checkboxes values in database.

    so what should i write in my query

    Code:
    $query = "insert into checkbox table (checkboxes) values()"
    vineet

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    design your table so that each checkbox value is a separate row
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hi r937

    i have 30 checkboxes in my form.

    so i have to make 30 rows or fields in my database.

    is there any alternative

    vineet

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    30 coumns or 30 rows if you want to use one value per column/row

    other alternatives are multiple values in a single column (which violates first normal form) or a bitmap (which requires application code to interpret)
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hi r937

    yes i would like to insert all selected checkboxes values in one single column with a comma separator.

    but that you say violates the form.

    normally what is adapted by programmers.

    i m new to it.

    vineet

  6. #6
    SitePoint Wizard guelphdad's Avatar
    Join Date
    Oct 2003
    Location
    St. Catharines, ON Canada
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    normally you would not store comma separated values. you would insert one new row for each value.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •