I have set up an update query which will update values entered into text fields on a while loop. This works fine until multiple data is being looped from the database. Then for some reason only the last data in the loop will be updated and the rest will stay the same.
<form method="post" action="update.php">
<?php
$id = $_POST["id"];
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$query= "SELECT * FROM list ORDER BY id ASC" ;
$result= mysql_query($query);
while($row = mysql_fetch_assoc($result) ){
echo"<input type=\"hidden\" name=\"id\" value=" . $row['id'] . " />";
echo"<input type=\"text\" name=\"fname\" value=" . $row['fname'] . " />";
echo"<input type=\"text\" name=\"lname\" value=" . $row['lname'] . " />";
}
?>
<input type="submit" value="Save Changes" />
<?php
$sql = "UPDATE list SET fname = '{$fname}', lname = '{$lname}' WHERE id = {$id}";
$result = mysql_query( $sql );
?>
</form>
This is because all of your inputs have the same name if there are more than one, so php can’t differentiate from one to another.
If there is more than 1 row generated from your select mysql query, then you need to give each id input a different name, each fname input a different name, and each lname input a different name.