#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    11
    Rep Power
    0
    I am attempting to use checkboxes to add multiple selections to my database.
    If I list each as:
    <P><input type="checkbox" name=rec value="Water Sports">Water Sports
    <P><input type="checkbox" name=rec value="Wildlife">Wildlife
    <P><input type="checkbox" name=rec value="Winter Sports">Winter Sports

    and check all three selections, only the last selection gets added to the database.

    If I use this format:
    <P><input type="checkbox" name=rec[] value="Water Sports">Water Sports
    <P><input type="checkbox" name=rec[] value="Wildlife">Wildlife
    <P><input type="checkbox" name=rec[] value="Winter Sports">Winter Sports

    Then only the word array gets posted to the database. I would like to be able to add multiple selections, and then be able to call this data from the database.
    Any suggestions? I am completely stumped!
    The present page I am using to enter data is here:
    http://camp-a-roo.com/database/add.shtml
    Thanks!



    ------------------
  2. #2
  3. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15

    and check all three selections, only the last selection gets added to the database.

    your variable is not an array so it can't hold the multiple values..

    ----

    Insted of giving this in check box use a multiple select drop down menu.

    <select size="5" name="rec[]" multiple>
    <option selected value="Water Sports
    ">Water Sports
    </option>
    <option value="Wildlife
    ">Wildlife
    </option>

    <option selected value="Winter Sports">Winter Sports</option>
    </select>

    -----------
    Once you save your record in a databse your data will saving with comma seperator like Wildlife,Winter Sports...

    i guess saving of the records won't be problem for you.Following is an example showing how you can dispaly back the values to the drop down menu from database.


    <?php

    $drop_down_values=array(Water Sports,Wildlife,Winter Sports);
    //First create an array with all the items of drop down menu.

    $con=mysql_connect("localhost","username","password");
    // connectivity string.

    mysql_select_db("databasename",$con);
    //select database
    $result=mysql_query("select field from tblname where some conditions ",$con);
    // pass a query to mysql.

    $row=mysql_fetch_array($result);
    //get the row values.

    $pieces = explode (",", $row["category"]);
    //separate the Item values.

    $drop_count=count($drop_down_values);
    //take the total count of dropdown items.

    $piece_count=count($pieces);
    //count of selected items, which we fetched from database.

    echo "<select size="5" name="rec[]" multiple>n";

    for($a=0;$a<$drop_count;$a++){
    //loop for populating selected values to the drop down.

    $repeat="0";
    //don't match the value more than once..

    for($b=0;$b<$piece_count;$b++){
    //loop for matching the selected item
    if ("$drop_down_values[$a]"=="$pieces[$b]"){
    // selected item matching with drop down item
    //make it as selected.
    echo "<option selected value="$drop_down_values[$a]">$drop_down_values[$a]</option>n";
    $repeat="1";
    //this value is matching, and already made it as selected.
    //so donít match this value again.
    }
    }

    if ($repeat=="0"){
    //No matching for drop down item...
    echo "<option value="$drop_down_values[$a]">$drop_down_values[$a]</option>n";
    }
    }

    echo "</select>n";
    //close the drop down.

    ?>

    you can implement the same logic with check box also..



    [This message has been edited by Shiju Rajan (edited July 01, 2000).]
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    11
    Rep Power
    0
    I set it up as you explained, but when I look in the data saved to my database - all it reads is 'array'.

    <BR><select size="5" name="REC[]" multiple>
    <option selected value="bike
    ">Biking
    </option>
    <option value="boat
    ">Boating
    </option>
    <option value="camp


    It does not give a list of the options I wish to enter into the database. Any more suggestions, or can you think of another way to enter a list of data options?

    Please help!

    Mary

    ------------------
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2000
    Posts
    669
    Rep Power
    15
    Your first try was correct, but you cannot just say "insert into dbtable rec='$rec'"

    You need to access each element of your array and create a string out of it before you insert into your db like:

    while (list ($key, $val) = each ($rec)) {
    $recreation .= $val .",";
    }

    Now after you loop through all elements of your array you should have a string called $recreation that is actually all elements of your array sepereated by a comma

Similar Threads

  1. Calculate the array values of checkbox results
    By schieb in forum ASP Programming
    Replies: 2
    Last Post: February 2nd, 2004, 10:46 PM
  2. query multiple selection boxes
    By zayin7232 in forum PHP Development
    Replies: 13
    Last Post: December 30th, 2003, 09:11 PM
  3. checkbox results to email question
    By ed shaw in forum PHP Development
    Replies: 14
    Last Post: December 28th, 2003, 10:30 PM
  4. checkbox selection
    By SimplyRed in forum HTML Programming
    Replies: 12
    Last Post: December 19th, 2003, 04:16 AM
  5. Inserting multiple checkbox values into the DB
    By ironmanx in forum PHP Development
    Replies: 14
    Last Post: December 5th, 2003, 06:39 PM

IMN logo majestic logo threadwatch logo seochat tools logo