www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: <select size="3" name="test" multiple> Multiple CTRL insert to database qu

  1. #1
    Join Date
    Jun 2004
    Location
    los angeles
    Posts
    324

    Question <select size="3" name="test" multiple> Multiple CTRL insert to database qu

    Hello!
    I am working on a web form and I have a multiple selection field

    PHP Code:
    <select size="3" name="selection" multiple>
    <
    option value="1">1</option>
    <
    option value="2">2</option>
    <
    option value="3">3</option>
    <
    option value="4">4</option>
    </
    select
    I am trying to insert the values into the database and as you notice this is a multiple selection form field select menu. My problem is that when is inserted into the database, only one selection gets posted. Is there anything that I have to do on the server side to input all the values? or the client side? Any suggestions/tips/help would be greatly appreciated!

  2. #2
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Name your form field with [] (eg. <select size="3" name="selection[]" multiple="multiple">). That will pass the values to PHP as an array. Now, you can loop through the array, and insert into the DB.

  3. #3
    Join Date
    Jun 2004
    Location
    los angeles
    Posts
    324

    ahh i see!

    okay, i get how it would work, but now i get an error

    [B]Notice: Array to string conversion in[B]

    i need to conver it to an array, I tried doing something like

    PHP Code:
    $real_selection trim(eregi_replace("'""''"$_POST['selection'])); 
    and it wouldnt pass the values. How would I make it so it the values get inserted into a PHP variable or array?

  4. #4
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    $_POST['selection'] is an array of all the selected items. You will need to loop through them, ala:

    PHP Code:
    foreach($_POST['selection'] as $sel) {
        echo 
    $sel# will output the current value


  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    Maybe...
    PHP Code:
    foreach($_POST['selection'] as $key => $value)
    {
      
    $real_selection[$key] = trim(eregi_replace("'""''"$value));

    (Just guessing, I have yet to work with a multiple select in PHP.)

    [Edit: I see Pyro beat me to the punch. ]
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Jun 2004
    Location
    los angeles
    Posts
    324
    that just outputs the values though right? I got a suggestion that goes something like this:

    PHP Code:
    <select size="3" name="selection[]" multiple
    <
    option value="1">1</option
    <
    option value="2">2</option
    <
    option value="3">3</option
    <
    option value="4">4</option
    </
    select>

    if(
    is_array($_POST['selection'])) { 
    $
    $real_selection serialize($_POST['selection']); 

    it is not passing the values to the variable because I think it is not recognizing the array. I am sure that I am inserting to the right function from the form right?

  7. #7
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Originally posted by vicpal25
    that just outputs the values though right?
    Yes, but you can do anything else that you'd like with them, there. The important part is the fact that you are looping through the array...

  8. #8
    Join Date
    Jun 2004
    Location
    los angeles
    Posts
    324
    nope. i tried that and it returns the value "array" into the database..i also tried inserting into different variables..I also tried the following

    PHP Code:
    if(is_array($_POST['selection'])) { 
    $real_selection serialize($_POST[selection]); 
    }

    INSERT INTO recruitfairs (selectionVALUES ('".mysql_escape_string($real_selection)."');"; 
    i get this funky value in my database:

    a:7:{i:0;s:3:\"Art\";i:1;s:11:\"Art History\";i:2;

  9. #9
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    That will just insert a serialized version of the array into the database. While technically it would work (you could unserialize it later), I would recommend looping through the array and running the SQL that way. If you describe what you want (how the data is to be stored in the DB), I can get you an example to look at, in the hopes that you'll be able to learn from it.

  10. #10
    Join Date
    Jun 2004
    Location
    los angeles
    Posts
    324
    well here is a live sample that i am working on:
    PHP Code:
    //this is the careerfair.php file 
    <form action="careerfairconf.php" name="careerform" method="post"  >
    <
    select size="3" name="desiredmajors[]" multiple>
    <
    option value="Accounting">Accounting</option>
    <
    option value="Advertising">Advertising</option>
    <
    option value="Art">Art</option>
    <
    option value="Art History">Art History</option>   
    </
    form
    --------------------------------------------

    PHP Code:
    // this is the careerfairconf.php file
    $connect = @odbc_connect($dsn$user$passwd);
    if(
    $connect) {

    if(
    is_array($_POST['desiredmajors'])) { 
    $real_desiredmajors serialize($_POST['desiredmajors']);  
    }
    $query "INSERT INTO recruitfairs (desiredmajors) VALUES ('".mysql_escape_string($real_desiredmajors)."');";

    $result odbc_exec($connect$query);
    odbc_close($connect);

    -----------------------------------------------
    This is posting into an access database with the following information:

    a:7:{i:0;s:3:\"Art\";i:1;s:11:\"Art History\";i:2;

    I can see the selections made but How do I get rid of the extra information. Perhaps something like:
    Art, Art History, etc. in the database would be proper.

  11. #11
    Join Date
    Nov 2004
    Posts
    155
    I've done something w/ multiple selects and have inserted them in a database comma delimmeted. aka Art, Art History, Biology, etc


    I've done it something like this:
    PHP Code:
    $real_selection "";
    foreach(
    $_POST['selection'] as $key => $value

      if (
    $real_selection)
        
    $real_selection .= ", ".trim($value);
      else
        
    $real_selection trim($value); 

    Then do your insert statement after the loop is finished.

    Hope this helps,
    Jaelan

  12. #12
    Join Date
    Jun 2004
    Location
    los angeles
    Posts
    324
    Dang, you rock Jaelan!

    I was going crazy finding out the solution for this and I was not looking outside the box! Nicely done! and it implements commas and stuff. Good work! Thanks.

  13. #13
    Join Date
    Nov 2004
    Posts
    155
    Glad to help

    Jaelan

  14. #14
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Jaelan - for future reference, since you aren't doing anything with the array key, you don't even need to assign it. You can run your foreach loop like this:

    PHP Code:
    foreach($_POST['selection'] as $value) { 

  15. #15
    Join Date
    Nov 2004
    Posts
    155
    Hah,
    Neat. Actually, I copied the foreach from above and just put my code in it. It shows I don't know much about 'foreach', but because it simplified my code, I'm changing it in my actual application. Thanks for the suggestion..

    Jaelan

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles