View Full Version : Anoher hidden fields question: Inserting these into a database?

07-10-2009, 05:40 PM
Hi All,

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

<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">

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?:


07-10-2009, 06:38 PM
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.

07-10-2009, 06:58 PM
Thanks, but 99 was just an example and I need to find out how to insert into the database as well - anyone else?

07-10-2009, 07:46 PM
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?

07-10-2009, 08:34 PM
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.

07-10-2009, 08:38 PM
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.