SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    ********* Addict DLG_1's Avatar
    Join Date
    Jun 2000
    Location
    Texas, USA
    Posts
    597
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how do I write a php3 statement that will take the multiple items selected from a form (list box) and input those as multiple rows in mysql?


    Thanks!


    Sam

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well if the multiple selct box is named somethink like

    $select[]

    Then

    for ($i=0;$i<count($select);$i++) {
    $result=mysql_query(sprintf("INSERT into tablename(field1) VALUES ('%s')", $select[$i]));
    }
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    ********* Addict DLG_1's Avatar
    Join Date
    Jun 2000
    Location
    Texas, USA
    Posts
    597
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    for ($i=0;$i<count($select);$i++) {
    $result=mysql_query(sprintf("INSERT into Lpromo (company_ID, promo_ID) VALUES ('$frmID', '%s')", $frmPromotionID[$i]));
    }



    this isn't working for me.


    Sam

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Becasue you didn't copy it right

    for ($i=0;$i<count($select);$i++) {
    $result=mysql_query(sprintf("INSERT into tablename(field1) VALUES ('%s')", $select[$i]));
    }

    See how the for statement has count($service) and the insert statement also has $service[$i]

    You used one name in one and a different in the other should be

    for ($i=0;$i<count($frmPromotionID);$i++) {
    $result=mysql_query(sprintf("INSERT into Lpromo (company_ID, promo_ID) VALUES ('$frmID', '%s')", $frmPromotionID[$i]));
    }
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    ********* Addict DLG_1's Avatar
    Join Date
    Jun 2000
    Location
    Texas, USA
    Posts
    597
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this only executes one time. the last number is input into the table.


    what's wrong?

    is the count($frmPromotionID) right? there isn't a php method to count the number of options?


    Thanks for any help.


    Sam

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It sounds to me like the select list isn't sending an array but just the last element in the select box make sure the name of the select element has the [] at the end like
    $frmPromotionID[]

    This will allow the array of the selected item to be passed to the php script correctly so then by couting the number of elements in the array is just like counting the number of elements selected in the select list, follow me?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    ********* Addict DLG_1's Avatar
    Join Date
    Jun 2000
    Location
    Texas, USA
    Posts
    597
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for ($i=0;$i<count($frmPromotionID[]);$i++) {
    $result=mysql_query(sprintf("INSERT into Lpromo (company_ID, promo_ID) VALUES ('$frmID', '%s')", $frmPromotionID[$i]));


    i put the [] after the select name in the for loop area...but i'm getting a fatal error.



    Sam

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The [] goes in the select statement like

    <select name="frmPromotionID[]">
    <option>...
    <option>...
    <option>...
    <option>...
    </select>

    It doesn't go in the for statement

    for ($i=0;$i<count($frmPromotionID);$i++) {
    $result=mysql_query(sprintf("INSERT into Lpromo (company_ID, promo_ID) VALUES ('$frmID', '%s')", $frmPromotionID[$i]));
    }

    Sorry if I am being confusing, this should be a very easy process. I use it all the time.

    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    ********* Addict DLG_1's Avatar
    Join Date
    Jun 2000
    Location
    Texas, USA
    Posts
    597
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, that made the difference.


    I'm giving you some "points".


    Sam

  10. #10
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah I see how my example may have been a bit misleading, sorry for the inconvience!
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  11. #11
    SitePoint Member
    Join Date
    Jan 2001
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just wanted to post a thank you to all that posted in this thread. i'm just learning php (switching from perl/cgi) and i've been banging my head constantly wondering why this thing wasn't working. i've ingested so much mountain dew over the past few days that i could have sworn that at one point it worked.

    anyway, thanks for the info on the arrays.

    also, there is a similar post for anyone viewing this site that was in the same situation as me: http://www.sitepointforums.com/showt...threadid=11238

    and while i'm at it ... here's how i used the code in my php program:

    $extras is a -select multiple- in a form that is passed to this php.


    $newextras="";
    /// put the array -extras- into a linear variable
    for ($i=0;$i<count($extras);$i++) {
    $newextras .= sprintf(" %s,", $extras[$i]);
    }
    $extras = $newextras;



    i handle it this way so i can save all the selections into one field in the table. then when the user is searching that field for their multiple selections ... i use the eregi() function to locate.


    if (eregi("$extras","$value"))


    once again ... thanks for the help ...



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
  •