【问题标题】:Looping through dynamic input boxes and inserting into mysql database循环动态输入框并插入mysql数据库
【发布时间】:2012-06-19 19:38:47
【问题描述】:

我的表单需要您的帮助。我正在尝试构建一个动态表单,其中在以前的表单 sp1.php 中输入的特定整数用于显示输入框的数量。

第一种形式的变量是 $state 和 $number。然后在处理页面sp2.php中,将$number的值放入for循环中,显示输入框。

我现在要做的是将输入到 tinput 框中的值插入到 mysql 表中。

我得到的错误是

1) 未定义索引:DISTRICT 2) 为 foreach() 提供的参数无效

请问我怎样才能使这项工作。谢谢。

我的代码如下。如果需要,我将非常乐意展示代码的其他部分。 谢谢。

<?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 mysql forms for-loop foreach


【解决方案1】:

这必须有效:

$count = count($_POST['district']);
for ($i=0; $i<$count; $i++){
    $district = $_POST['district'][$i];
    //do this
    //do that
}

【讨论】:

  • 尝试打印 $_POST 的值并检查您是否获得了该地区或其他任何信息。
  • 试试这个:print_r($_POST);
  • 我已经做到了.. 现在数组已经形成并显示如下数组([distinct]=> Array ([0]=> [1]=>)) [submit]=>Register ) 这是我所期待的。但我仍然得到未定义的地区索引。
  • 您上一条评论中的 "array([distinct]=>" 是错字吗,还是实际上是不同的,而不是地区?
猜你喜欢
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
  • 2013-08-15
  • 1970-01-01
  • 2019-10-18
  • 2012-04-23
  • 2015-06-21
  • 2020-07-23
相关资源
最近更新 更多