SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple Database Entries from One Form Submission?

    Lets say I have a newspaper type database.

    I have an article table with the following columns:

    Article_ID
    Article_Name

    Then I have a Article Category Table with the following Columns:

    Category_ID
    Category_Name


    Then I have another table that links to two together with the following columns:

    Article_ID
    Category_ID


    I want a make a form that displays all of the Categories with a select statement that delivers code similar to the following so that I can select which category the News Article can go to:

    <input type="checkbox" name="option1" value="(category_id_1)"> News<br>
    <input type="checkbox" name="option2" value="(category_id_2)"> Sports<br>
    <input type="checkbox" name="option2" value="(category_id_3)"> Weather<br>
    For each option checked I want to put a seperate entry in the third table I mentioned.

    So lets say that I am adding an article. The article ID ends up being 557 and I select Sports and Weather I want to add two entries with the following data:

    Article_ID - Category_ID
    557 - 2
    557 - 3

    What is the best way for me to go about doing this?

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Change the name attribute on your checkboxes:

    PHP Code:
    <input type="checkbox" name="option[]" value="(category_id_1)"News<br>
    <
    input type="checkbox" name="option[]" value="(category_id_2)"Sports<br>
    <
    input type="checkbox" name="option[]" value="(category_id_3)"Weather<br
    Now, when you process the form submission, you can access 'option' as an array:

    PHP Code:
    $options = !empty($_POST['option']) ? $_POST['option'] : array();
    foreach(
    $option as $opt) {
        
    // $opt now contains the category_id
        // insert relation into database


    The trick here is the name of the checkbox (option[]). That makes PHP parse it as an array when the form is submitted

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    In addition to immerse's answer, you should create an insert statement that inserts all rows in one go, avoiding multiple database calls.

  4. #4
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How would you do that?

  5. #5
    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)
    INSERT INTO article_categories (article_id,category_id) VALUES
    ( 557,2 ) , ( 557,3 ) , ...

    one sql statement, multiple rows inserted
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •