I have a part of webpage that is dynamically generated using MySQL queries:
<table id="livePlayers" cellpadding="4" align="center">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone number</th>
<th>Email Address</th>
<th>Rating</th>
</tr>
<?php
while ($currLine = @mysql_fetch_array($resultSet))
{
$phoneCount= "phone".$count;
$ratingCount= "rate".$count;
?>
<tr>
<td><?php echo $currLine[1]?></td>
<td><?php echo $currLine[2]?></td>
<td><input type="text" name="<?php echo $phoneCount ?>"></td>
<td><?php echo $currLine[0]?></td>
<td><input type="text" name="<?php echo $ratingCount ?>"></td>
</tr>
<?php
$count++;
}
?>
</table>
As you can see, 2 fields are of input type="text". Once the user has made entries in these fields, I need to update the database table with these values on clicking Submit.
However, I don’t understand how to do this since the field name would be dynamically generated. Is there any way to update the respective rows with entered values?
The
nameof an input should not be generated dynamically. Instead use a constant. The dynamic value of that input should be set in thevalueattribute of the input.Example for one of your inputs:
You can retrieve the value by using the $_GET or $_POST arrays. Assuming your form uses the POST method you can do the following:
Now you can write your UPDATE mysql query.
Update
To update more than one row at the same time, use an array as POST parameter:
Retrieving
$_POST['phone']will give you an array of all rows then.Update 2
To identify each row you can use hidden fields like so:
Or you use the id as associative array key for all other input fields: