I have a table within a form (new_tear.php) that takes data from a single user. The first cloumn is populated from a database table (ProfileTable) with names. The user has to select an option from a dropdown for each row (each name). When the form is submitted (POST to tear_done.php), I want to store each row in my database. However, I am only seeing the last row posted each time
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++;
}
?>
What I am getting from the echo is the sql with all the names and dept correct, however the other fields $DZ_M, $DZ_T etc only repeat the last row entered from the table in new_tear.php. What is it am I missing please?