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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts

    Anoher hidden fields question: Inserting these into a database?

    Hi All,

    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
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    yes why not.
    If you know the number of records in the html supposedly 99.
    you loop over 99
    String name = null;
    for (int i=0; i<99; i++)
    {
    name = request.getParameter("name_"+ (i+1));
    }

    this is java code, you can do the same in the language you are using.

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts
    Thanks, but 99 was just an example and I need to find out how to insert into the database as well - anyone else?

  • #4
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    I know it is an example and I used the same example so you retrieve all the values in one loop.

    Besides inside the looping I have done and for each value of i you will do the following:
    String name = null;
    String amount = null;
    String quantity = null;
    //open connection
    for (int i=0; i<99; i++)
    {
    name = request.getParameter("name_"+ (i+1));
    amount = request.getParameter("amount _"+ (i+1));
    quantity = request.getParameter("quantity _"+ (i+1));
    // write your query
    // execute the query

    }

    close the connection.

    This the algorithm I have written to you, there are many more , but this is one of them.
    Besides what language you are using so we help in the code?

  • #5
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by ckeyrouz View Post
    Besides what language you are using so we help in the code?
    Um, you're posting in the PHP forum. I'll give you one guess which language he's using.

    And your algorithm is still lacking because you have the hard-coded '99'. What stfc_boy failed to mention is that there will be an arbitrary number of variables because they're being built by the variable variable technique.

    stfc_boy, I might follow something like ckeyrouz's logic as far as using the base name of your variables as a common "hook" to grab a count of the total for the current order. Then you could adapt his algorithm to fit that count. There's also the more complicated option of dumping all of these into a multi-dimensional array and querying from there.
    Last edited by tomws; 07-10-2009 at 07:58 PM. Reason: clarification, spelling
    Are you a Help Vampire?

  • #6
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    Well in fact I did not pay attention which forum is that because I looked into new posts and you do not have to be sarcastic about it.

    you are still insisting about the 99 while I mentioned it is an example and I do not write static code.

    Depending on how he is building data he can do the counting.
    There are million ways all he has to do is to choose one.

    I thought the problem was in the data catching not how to get the number of records.

    Hope this is clear.


  •  

    Posting Permissions

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