Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2006
    Location
    Vienna, VA
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding multiple records to a table in one form.

    Okay, was not quite sure what topic to post this on.

    I have one form in a php page. In that form there are several rows, each with several input boxes. These rows represent individual products and their price. You may add as many products as you want by clicking a add product button and it will add another row using javascript. When you are finished adding your products you then click submit. For each product I would like it to insert an individual record in the products table.

    For example if you add 10 products, when you click submit it will add 10 records in the product table.

    Can you give me some direction in the matter.

    Thanks

  • #2
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,631
    Thanks
    4
    Thanked 147 Times in 138 Posts
    so in your form you would have some kind of place holder so you know how to identify each row right? say rowid or whatever?

    you would then do one of two things, either create an array that holds all of your values and then in your sql you would step through the array to insert each row OR you would create a loop and do a single sql insert for each row.

  • #3
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You need to give the textbox the Record ID, or something that will uniquely identify it.

    Because HTML requires a letter to start a name or ID then you will have to name your boxes accordingly and then split the name after submit.

    PHP Code:
    <form name="form1" method="post" action="">
      <p>
        <input name="id_1" type="text" id="id_1" value="30">
    </p>
      <p>
        <input name="id_23" type="text" id="id_23" value="50">
        <input name="text" type="text" id="id_23" value="tezxt">
      </p>
      <p>
        <input name="id_45" type="text" id="id_45" value="4">
      </p>
      <p>
        <input type="submit" name="Submit" value="Submit">
    </p>
    </form>
    <?php
    if(isset($_POST['Submit'])){
        foreach(
    $_POST as $key => $val){
            if(
    strpos($key'id_') !== false){;
                
    $id split('id_'$key);
                
    //echo $id[1] . ': ' . $val . '<br>';
                
    $sql "INSERT INTO table (id, amount) VALUES (" $id[1] . "'," $val ")";
                echo 
    $sql '<br>';
            }
        }
    }
    ?>


  •  

    Posting Permissions

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