Ayush Rustagi Ayush Rustagi - 4 months ago 16
HTML Question

On submit post last value fetched through while loop

Whenever I click submit the last value of email fetched is always posted.
what should I do to post the current email, and how to post the current email value with the selected status from each table row

<form action="" method="post">
<table>
<tbody>
<?php
while($row=mysqli_fetch_assoc($query))
{
echo '
<tr>
<td style="text-align:center;">
<select name="statuss">
<option selected="selected" disabled="disabled">Select Status</option>
<option value="1">Submitted</option>
<option value="2">Accepted</option>
<option value="3">Questionnaire</option>
<option value="4">Interview</option>
</select>
<input type="text" name="email" value="'.$row['email'].'">
<input type="submit" value="Update" name="submit" class="btn btn-primary">
</td>
</tr>';
}
?>
</tbody>
</table>

</form>
<?php
if(isset($_POST['submit']))
{
$email=(isset($_POST['email']) ? $_POST['email'] : '');
$statuss=(isset($_POST['statuss']) ? $_POST['statuss'] : '');
$sql="update status SET personal_stylist = '$statuss' where email='$email'";
$query=mysqli_query($connection, $sql);
}
?>

Answer

Among a number of other problems with the code (which you drastically change with every edit, so it's hard to keep up), logically the form has no way of knowing which values you intuitively intended to post.

Simplified, you have this structure:

<form>
  <table>
    <row>a bunch of fields</row>
    <row>a bunch of fields</row>
    <row>a bunch of fields</row>
  </table>
</form>

However, each row has the same fields. So when the form posts, it can't know which fields you were looking at when you submitted the form. All it can do is package them all up into one form POST. And as it does so, each time it encounters a field by the same name, the new field will overwrite the previous field.

Move your <form> to inside the rows. Simplified again, the structure would be more like this:

<table>
  <row>
    <form>a bunch of fields</form>
  </row>
  <row>
    <form>a bunch of fields</form>
  </row>
  <row>
    <form>a bunch of fields</form>
  </row>
</table>

More specifically, make the <form> a descendant of the <td>:

while($row=mysqli_fetch_assoc($query))
{
  echo '
  <tr>
    <td style="text-align:center;">
      <form>
        your form
      </form>
    </td>
  </tr>';
}