www.webdeveloper.com
Results 1 to 7 of 7

Thread: Looping through array and insert into Db

  1. #1
    Join Date
    Jan 2005
    Location
    UK
    Posts
    381

    Looping through array and insert into Db

    Hi,

    I have a select menu below, when I submit the form, I need to loop through each option and insert it into a table using a query.

    Can anybody tell me how to do it?

    Thankyou

    HTML Code:
    <select name="SubscriptionOptions[]" multiple="multiple">
    <option value="Product Announcements" />Product Announcements</option>
    <option value="Website Updates" />Website Updates</option>
    <option value="Upcoming Seminars" />Upcoming Seminars</option>
    <option value="Changes to Privacy Policy" />Changes to Privacy Policy</option>
    </select>

  2. #2
    Join Date
    Dec 2005
    Posts
    25
    I'm not sure, what you can't handle, arrays or database queries, but here's how to manage the array...

    PHP Code:
    $array $_REQUEST['SubscriptionOptions'];    
    foreach(
    $array as $value) {
        
    //insert into database    


  3. #3
    Join Date
    May 2003
    Location
    Pensacola, FL
    Posts
    635
    you can have only one item selected, so no looping is needed.

    your posted form (or get) will produce the following:

    PHP Code:
    $_POST['SubscriptionOptions[]']=='chosen options value';
    // or get
    // $_GET['SubscriptionOptions[]']=='chosen options value'; 
    why do you have the [] in there? doesn't the plural imply that it is more than one anyway?

    then, i'd make it easy on you and say:

    PHP Code:
    $sub $_POST['SubscriptionOptions[]'];
    // error check, add slashes or whatever
    // add to database 
    it's only one result your getting back, not an array...

    hope that helped.

  4. #4
    Join Date
    Sep 2005
    Posts
    114
    Quote Originally Posted by JDM71488
    you can have only one item selected, so no looping is needed.
    Wrong you can post as many from the selection as you like that's what multiple means.
    Quote Originally Posted by JDM71488

    your posted form (or get) will produce the following:

    PHP Code:
    $_POST['SubscriptionOptions[]']=='chosen options value';
    // or get
    // $_GET['SubscriptionOptions[]']=='chosen options value'; 
    why do you have the [] in there? doesn't the plural imply that it is more than one anyway?
    The [] is there so that each of the options can be added to the array
    Quote Originally Posted by JDM71488

    then, i'd make it easy on you and say:

    PHP Code:
    $sub $_POST['SubscriptionOptions[]'];
    // error check, add slashes or whatever
    // add to database 
    it's only one result your getting back, not an array...
    Then you'd be wrong again It IS an array.
    Quote Originally Posted by JDM71488
    hope that helped.
    Probably not!
    Quote Originally Posted by mattastic
    I have a select menu below, when I submit the form, I need to loop through each option and insert it into a table using a query.

    Can anybody tell me how to do it?
    This may help
    PHP Code:
    <?php

    if (isset($_POST['submit'])){
        
    //your normal DB connection script here

        
    foreach ($_POST["SubscriptionOptions"] as $key => $value) {
            
    $result mysql_query('INSERT INTO table_name field_name VALUES(\''.$value.'\')');
            if (!
    $result ) die ('Insert failed');
        }


    } else {

    ?>
    <form action="" method="POST">
    <select name="SubscriptionOptions[]" multiple="multiple">
    <option value="Product Announcements" />Product Announcements</option>
    <option value="Website Updates" />Website Updates</option>
    <option value="Upcoming Seminars" />Upcoming Seminars</option>
    <option value="Changes to Privacy Policy" />Changes to Privacy Policy</option>
    </select>
    <input type="submit" name="submit" value="Submit">
    </form>
    <?php
    }
    ?>
    it hasn't been tested but should lead you in the right direction.

  5. #5
    Join Date
    May 2003
    Location
    Pensacola, FL
    Posts
    635
    instead of being an a__ about it like somebody, i'll explain why i said what i did...

    i tried using my first select element with the multiple attribute this morning when i made my post. it showed all for options and when i left clicked on each one, it only highlighted one. that's why i thought that you could still only choose one. i guess you'd have to use the ctrl and select keys respectively...

    however after learning that it did allow you to select multiple i do understand why you would need a loop and why the resulting variable would have to be an array.

    im sorry for my post, i was wrong about multiple...

  6. #6
    Join Date
    Sep 2005
    Posts
    114
    Quote Originally Posted by JDM71488
    instead of being an a__ about it like somebody, i'll explain why i said what i did...
    Hey no need to get in a snit about it, just be careful about making definitive statements about something you know little about.

    BTW if you don't know what [] is for I would suggest you read the manual about arrays

  7. #7
    Join Date
    May 2003
    Location
    Pensacola, FL
    Posts
    635
    Quote Originally Posted by rincewind456
    BTW if you don't know what [] is for I would suggest you read the manual about arrays
    Quote Originally Posted by JDM71488
    however after learning that it did allow you to select multiple i do understand why you would need a loop and why the resulting variable would have to be an array.
    (10char)

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