开发者

Retrieving multiple rows from a loop-created form... Stuck

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='Dai开发者_JS百科ly'>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.


Tried this?

<input name="description[]" type="text" ... />
<input name="option_id[]" type="text" ... />
<select name="assisted_frequency[]">...</select>

and then

$expectNumRows = ...;
function is_good_subm_data($name, $num) {
    return (array_key_exists($name, $_POST) &&
        is_array($_POST[$name]) && count($_POST[$name]) == $num);
}
if (!is_good_subm_data('description', $expectNumRows) ||
    !is_good_subm_data('option_id', $expectNumRows) ||
    !is_good_subm_data('assisted_frequency', $expectNumRows)) {
    //error handling
}

for ($i = 0; $i < $expectNumRows; $i++) {
    //read $_POST['description'][$i] etc.
}

Very basic (no validation) PHP file:

<?php
if ($_SERVER['REQUEST_METHOD'] == "GET") {
?>
<form method="post">
Description 1: <input name="description[]" type="text"  /><br />
Option Id 1: <input name="option_id[]" type="text" /><br />
<br />
Description 2: <input name="description[]" type="text"  /><br />
Option Id 2: <input name="option_id[]" type="text" /><br />
<input type="submit" />
</form>
<?php } else {
    $expectNumRows = 2;
    for ($i = 0; $i < $expectNumRows; $i++) {
        echo "description $i:" .
            htmlentities($_POST["description"][$i]) . "<br />";
        echo "option id $i:" .
            htmlentities($_POST["option_id"][$i]) . "<br />";
        echo "<br />";
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜