gpt4 book ai didi

php - 从循环创建的表单中检索多行...卡住了

转载 作者:行者123 更新时间:2023-11-29 01:01:28 26 4
gpt4 key购买 nike

首先让我说我是 PHP 的新手,但我是来这里学习的,非常感谢您的帮助。

我使用以下代码提取数据并创建表单。这会创建最多 40 行供用户填写。每行包含相同的信息:描述、数量和频率。所需的其余信息由数据库生成。 (查看隐藏字段)

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

我在检索用户输入的信息时遇到问题。我的目的是在用户输入信息后遍历每一行,然后将我的数据库信息和用户信息的混合上传到另一个数据库中。例如,用户会看到,尽管更漂亮:

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

提交上面的表单后,我使用类似于以下的查询将数据输入数据库:

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
}

我尝试过 for, foreach, arrays(感觉就像我所知道的一切)将每一行(逐行)发布到数据库中。我要么只得到最后一行数据,要么根本没有数据。我还担心 [$row] 技术会向我的数据中添加字符。

检索用户输入的每一行,然后将此数据(逐行)上传到数据库的最佳方法是什么?另外,非常感谢您提出的改进我的编码技术和我正在采用的方法的建议。

最佳答案

试过这个吗?

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

然后

$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.
}

非常基本(无验证)的 PHP 文件:

<?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 />";
}
}

关于php - 从循环创建的表单中检索多行...卡住了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2831523/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com