我的php表单只保存了我表格的最后一行。

时间:2018-05-10 17:35:11

标签: php html mysql

我在表单(new_tear.php)中有一个表,它从单个用户获取数据。第一个cloumn是从具有名称的数据库表(ProfileTable)填充的。 用户必须从每个行(每个名称)的下拉列表中选择一个选项。 提交表单时(POST到tear_done.php),我想在我的数据库中存储每一行​​。 但是,我每次只看到最后一行

new_ter.php

<?php
$sqlT1 = "SELECT * FROM ProfileTable WHERE dept = 'T1'";
$queryT1 = mysqli_query($conn, $sqlT1);
if (!$queryT1) {
  die ('SQL Error: ' . mysqli_error($conn));
}
?>
<form action="tear_done.php" method="post">
<table>
<?php
    $no     = 1;
    while ($row = mysqli_fetch_array($queryT1))
    {
        echo '<tr>
                <td>'.$no.'</td>
                <td>'.$row['name'].'</td>
                <td><select id="Dz_M" name="Dz_M">
                    <option value="4pm">4pm</option>
                    <option value="8am">8am</option>
                    <option value="Rest">Rest</option>
                </select><br>
                </td>
                <td><select id="Dz_T" name="Dz_T">
                    <option value="4pm">4pm</option>
                    <option value="8am">8am</option>
                    <option value="Rest">Rest</option>
                </select><br>
                </td>
                <td><select id="Dz_W" name="Dz_W">
                    <option value="4pm">4pm</option>
                    <option value="8am">8am</option>
                    <option value="Rest">Rest</option>
                </select><br>
                </td>
                <td><select id="Dz_F" name="Dz_F">
                    <option value="4pm">4pm</option>
                    <option value="8am">8am</option>
                    <option value="Rest">Rest</option>
                </select><br>
                </td>
                <td><select id="Dz_S" name="Dz_S">
                    <option value="4pm">4pm</option>
                    <option value="8am">8am</option>
                    <option value="Rest">Rest</option>
                </select><br>
                </td>                   
                </tr>';
        $no++;
        $no_cont = $no;
    }?> 
</table>
<br>
<button value="save" type="submit" name="save">Save Tear</button>       
</form>

tear_done.php

<?php
$sqlT1 = "SELECT * FROM ProfileTable WHERE dept = 'T1'";
$queryT1 = mysqli_query($conn, $sqlT1);
if (!$queryT1) {
    die ('SQL Error: ' . mysqli_error($conn));
}
$no = 1;
while ($row = mysqli_fetch_array($queryT1))
{
$Name = $row ['name'];
$Dept = $row ['dept'];
$DZ_M = $_POST ["Dz_M"];
$DZ_T = $_POST ["Dz_T"];
$DZ_W = $_POST ["Dz_W"];
$DZ_F = $_POST ["Dz_F"];
$DZ_S = $_POST ["Dz_S"];

$sql = "INSERT INTO TearTable (name, Mdy, Tdy, Wdy, Fdy, Sdy, dept)
VALUES ('$Name', '$DZ_M', '$DZ_T', '$DZ_W', '$DZ_F', '$DZ_S', '$Dept')";    

echo $no;
echo '<br>';
echo $sql; //for testing
echo '<br>';
$no++;
}   
?>

我从echo获得的是具有所有名称和部门的sql,但是其他字段$ DZ_M,$ DZ_T等仅重复从new_tear.php中的表中输入的最后一行。 我错过了什么?

0 个答案:

没有答案