SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Insert in db row by row using hidden fields?

    Quick question. Say I had my list of hidden fields like so:

    Code:
      <form action="collect/" method="post"> 
    
        <input type="hidden" name="item_name_1" value="Shirt">
        <input type="hidden" name="amount_1" value="9.99">
        <input type="hidden" name="quantity_1" value="1">    
                                      
        <input type="hidden" name="item_name_2" value="Hat">
        <input type="hidden" name="amount_2" value="9.99">
        <input type="hidden" name="quantity_2" value="1">
    
        <input type="hidden" name="item_name_3" value="Hat">
        <input type="hidden" name="amount_3" value="9.99">
        <input type="hidden" name="quantity_3" value="1">
    
        <input class="paybtn" type="submit" name="Submit" value="submit">
    </form>

    And these could go up to a big number i.e. item_name_99, amount_99, quantity_99 for example.

    Is it possible to grab all of these values and INSERT them into a database like so, upon submit row-by-row?:

    Items
    -------------
    id
    name
    amount
    quantity

  2. #2
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean something like this?

    PHP Code:
      <form action="collect/" method="post"> 

      <?php
      
    for ($i 0$i 99$i++) {
      
    ?>
        <input type="hidden" name="item_name_<?php echo $i?>" value="Shirt">
        <input type="hidden" name="amount_<?php echo $i?>" value="9.99">
        <input type="hidden" name="quantity_<?php echo $i?>" value="1">    
                                      
    <?php ?>
        <input class="paybtn" type="submit" name="Submit" value="submit">
    </form>
    Sorry if i misunderstood.

    I think you have to do separate MySQL Queries to insert each new set unless you loop it with those values, or else you will need to automatically generate 0 - 99 in your MySQL Insert.

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Here's it explained a bit better hopefully. The number 99 was just an example it could be any number. So basically I want to grab:

    item_name_(anynumber)
    amount_(anynumber)
    quantity_(anynumber)


    And insert them into a database like so

    PHP Code:

    // Update the new values in the database
    if(isset($_POST['Submit']))
    {

    //DO SOMETHING HERE THAT WILL ASK IT TO INSERT ROW BY ROW??
    {

    $SQL 
    "INSERT INTO tbl
    (item_name,amount,quantity) 
    VALUES
    ('?????', '??????', $????')"
    ;

    }


    Row by row?

    But i'm not sure how to do this?

  4. #4
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris_stfc View Post
    Thanks, Here's it explained a bit better hopefully. The number 99 was just an example it could be any number. So basically I want to grab:

    item_name_(anynumber)
    amount_(anynumber)
    quantity_(anynumber)


    And insert them into a database like so

    PHP Code:

    // Update the new values in the database
    if(isset($_POST['Submit']))
    {

    //DO SOMETHING HERE THAT WILL ASK IT TO INSERT ROW BY ROW??
    {

    $SQL 
    "INSERT INTO tbl
    (item_name,amount,quantity) 
    VALUES
    ('?????', '??????', $????')"
    ;

    }


    Row by row?

    But i'm not sure how to do this?
    the name of the hidden values must be in an array like item_name[],amount[] and quantity[]
    and then use foreach for one field to get them one by one
    also use a counter for the remaining two
    hope u understood


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
  •