Php 通过动态输入框循环并插入mysql数据库
我需要你帮我填表格。我正在尝试构建一个动态表单,其中使用在以前的表单sp1.php中输入的特定整数来显示输入框的数量 第一种形式的变量是$state和$number。然后在处理页面sp2.php上,将$number的值放入for循环中以显示输入框 我现在要做的是将输入tinput框的值插入到mysql表中 我得到的错误是 1) 未定义索引:地区 2) 为foreach()提供的参数无效 请问我如何才能使这项工作。多谢各位Php 通过动态输入框循环并插入mysql数据库,php,mysql,forms,for-loop,foreach,Php,Mysql,Forms,For Loop,Foreach,我需要你帮我填表格。我正在尝试构建一个动态表单,其中使用在以前的表单sp1.php中输入的特定整数来显示输入框的数量 第一种形式的变量是$state和$number。然后在处理页面sp2.php上,将$number的值放入for循环中以显示输入框 我现在要做的是将输入tinput框的值插入到mysql表中 我得到的错误是 1) 未定义索引:地区 2) 为foreach()提供的参数无效 请问我如何才能使这项工作。多谢各位 <?php $state=htmlspecialchars((
<?php
$state=htmlspecialchars(($_POST['state'])) ;
$number = intval(($_POST['number']));
for ($i = 0; $i < $number ; $i++ ) {
echo "
<form action='sd2.php' method='post'>
<label for='name'>Districts</label>
<input type='text' name='district[]'>
<br/><br/>
</form>";
}
?>
<?php
foreach($_POST['district'] as $senatorial) {
$query = "INSERT INTO state ( `state_id`, `state`, `senatorial`)
VALUES (NULL, '".$state."', '".$senatorial."') ";
mysql_query($query) or die (mysql_error());
}
?>
我的代码在下面。。如果需要,我将非常乐意展示代码的其他部分。
多谢各位
<?php
$state=htmlspecialchars(($_POST['state'])) ;
$number = intval(($_POST['number']));
for ($i = 0; $i < $number ; $i++ ) {
echo "
<form action='sd2.php' method='post'>
<label for='name'>Districts</label>
<input type='text' name='district[]'>
<br/><br/>
</form>";
}
?>
<?php
foreach($_POST['district'] as $senatorial) {
$query = "INSERT INTO state ( `state_id`, `state`, `senatorial`)
VALUES (NULL, '".$state."', '".$senatorial."') ";
mysql_query($query) or die (mysql_error());
}
?>
这必须起作用:
$count = count($_POST['district']);
for ($i=0; $i<$count; $i++){
$district = $_POST['district'][$i];
//do this
//do that
}
$count=count($_POST['district']);
对于($i=0;$i尝试打印$\u POST的值,并检查是否获得了地区或其他任何内容。尝试以下操作:打印$\r($\u POST);我已完成此操作..现在数组的格式和显示方式如下([distinct]=>array([0]=>[1]=>)[submit]=>Register),这正是我所期望的。但我仍然得到了未定义的地区索引。您上次评论中的“array([distinct]=>”是一个打字错误,还是它实际上是独特的,而不是地区?所以您知道:标记的for属性应该等于相关元素的id属性,以便将它们绑定在一起。