Retrieving multiple rows from a loop-created form... Stuck.
Posted
by hangston
on Stack Overflow
See other posts from Stack Overflow
or by hangston
Published on 2010-05-14T02:34:13Z
Indexed on
2010/05/14
3:14 UTC
Read the original article
Hit count: 179
Let me start by saying that I'm new to PHP, but I'm here to learn and would really appreciate your help.
I use the following code to pull in data and create a form. This creates up to 40 lines for a user to fill out. Each line consists of the same information: Description, Amount, and Frequency. The remainder of the information needed is generated by the database. (See hidden fields)
<?php
$row = 0;
do {
$optid = $row_options['option_id'];
echo "<tr>\n\t<td>" . htmlentities($row_options['option']) . "</td>\n";
echo "\t<td>" . "<input name='description' type='text' size='40' maxlength='120'/>" . "</td>\n";
echo "\t<td>" . "<input name='option_id' type='hidden' value='$optid' />$<input name='amount' type='text' size='10' maxlength='7'/>" . "</td>\n";
echo "\t<td>" . "<select name='assisted_frequency'>
<option value='Monthly'>Monthly</option>
<option value='Weekly'>Weekly</option>
<option value='Daily'>Daily</option>
<option value='Hourly'>Hourly</option>
<option value='One-Time'>One-Time</option>
</select>" . "</td>\n</tr>\n";
$array[$row] = array(
$arraydesc[$row] = $_POST['description'],
$arrayamto[$row] = $_POST['amount'],
$arrayoptid[$row] = $optid,
$arrayfreq[$row] = $_POST['frequency'],
);
$row ++;
} while ($row_options = mysql_fetch_assoc($options));
$counter = $row - 1;
?>
I'm having troubles retrieving the information that the user inputs. My intent is to loop through each row after the user has input their information, then upload the mix of my database information and the user's information into another database. For example, the user would see, albeit prettier:
form1
Option 1: description [input box] amount [input box] frequency [option box]
Option 2: description [input box] amount [input box] frequency [option box]
Option 3: description [input box] amount [input box] frequency [option box]
Option 4: description [input box] amount [input box] frequency [option box]
submit
Upon submitting the form above, I'm using a query similar to the following to input the data into the database:
for($row=0; $row<=$counter; $row++){
$insertSQL2 = sprintf("INSERT INTO table (option_id, amount, description, frequency) VALUES (%s, %s, %s, %s)",
GetSQLValueString($arrayoptid[$row], "int"),
GetSQLValueString($arrayamto[$row], "int"),
GetSQLValueString($arraydesc[$row], "text"),
GetSQLValueString($arrayfreq[$row], "text"));
// code to submit query
}
I've tried for, foreach, arrays (what feels like the everything I know) to post each row (row by row) into the database. I either get just the last row of data, or no data at all. I also worry that the [$row] technique is adding characters to my data.
What is the best way to retrieve each row of the user's inputs, then upload this data (row by row) into the database? Also, I would really appreciate your suggestions for improving my coding technique and the approach I'm taking.
© Stack Overflow or respective owner