无法在数组的下一个循环中存储数据

时间:2018-08-31 10:48:38

标签: php mysql

我不知道这段代码有什么问题,但是每当我选择填写多个表格时,将仅显示数据的最后一部分。例如,我会选择3个表格,并仅在存储最后一个表格时用数据填充它。我使用此代码显示了应该在数组中的数据。

echo "<pre>";                                                
print_r($_POST);                                                         
echo "</pre>";

这是我的代码,请帮助我

<?php

//  $_POST['countEmp'] is a variable from another page that would determine 
//  how many loops that it should execute  

$count = $_POST['countEmp'];
$sql = "SELECT * FROM position";
$res = mysqli_query($conn,$sql);
$flag = 0;

for ($i=0; $i < $count; $i++) { 
    echo " <form method='POST' action=insertProject1.php> ";
    echo " <input type='text' name='employee[]' placeholder='employee's name'> ";
    echo "<select  name='position[]'>";
    echo "<option selected='selected'>POSITION</option>";
       while ($data = mysqli_fetch_assoc($res)) {
           echo "<option value=".$data['position_id'].">".$data['position']."</option>";
        }   
    echo "</select>";
    echo "<br>";
    $flag++;
    if ($flag== $count) {
        echo "<input type='submit' value='SUBMIT' name='insert'>";
    }
    mysqli_data_seek($res,0);
    echo " </form> ";
    }
?>

1 个答案:

答案 0 :(得分:0)

由于您使用自己的提交按钮拥有单独的表单,因此提交的数据将仅属于单击的提交按钮的表单。您将需要重组/合并表格以产生所需的结果。

这是我在本地主机上进行测试的方式:

<?php
var_export($_POST);
?>
<br>
<form method='POST' action=''>
    <input type='text' name='employee[]' placeholder='employee&apos;s name'>
    <select  name='position[]'>
        <option>POSITION</option>
        <option value="1">A</option>
        <option value="2">B</option>
        <option value="3">C</option>
    </select>
    <br>
    <input type='submit' value='SUBMIT' name='insert1'>";
</form>
<br>
<form method='POST' action=''>
    <input type='text' name='employee[]' placeholder='employee&apos;s name'>
    <select  name='position[]'>
        <option>POSITION</option>
        <option value="1">A</option>
        <option value="2">B</option>
        <option value="3">C</option>
    </select>
    <br>
    <input type='submit' value='SUBMIT' name='insert2'>";
</form>
<br>
<form method='POST' action=''>
    <input type='text' name='employee[]' placeholder='employee&apos;s name'>
    <select  name='position[]'>
        <option>POSITION</option>
        <option value="1">A</option>
        <option value="2">B</option>
        <option value="3">C</option>
    </select>
    <br>
    <input type='submit' value='SUBMIT' name='insert3'>";
</form>

连同employee[]position[]值,当我单击第二个提交按钮时,我看到了$_POST['insert2'] = "SUBMIT"


如果您想要这样的结构:

array (
    'employee' => array ( 0 => 'Frank', 1 => 'Jim', 2 => 'Earl', ),
    'position' => array ( 0 => '2', 1 => '1', 2 => '3', ),
    'insert' => 'SUBMIT',
)

然后用一个提交按钮将所有字段包装为一种形式。


或者如果您想要类似的结构:

array (
   'set' => array (
       0 => array ( 'employee' => 'Candice', 'position' => '3', ),
       1 => array ( 'employee' => 'Bert', 'position' => '2', ),
       2 => array ( 'employee' => 'Abe', 'position' => '1', ),
   ),
   'insert' => 'SUBMIT',
)

然后,您可以构建单个表单,如下所示:

<form method='POST' action=''>
<?php
for ($i = 0; $i < 3; ++$i) {
    echo "<input type='text' name='set[$i][employee]' placeholder='employee&apos;s name'>";
    echo "<select name='set[$i][position]'>";
        echo "<option>POSITION</option>";
        echo "<option value='1'>A</option>";
        echo "<option value='2'>B</option>";
        echo "<option value='3'>C</option>";
    echo "</select>";
    echo "<br>";
}
?>
<input type='submit' value='SUBMIT' name='insert'>
</form>