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 ech开发者_StackOverflow中文版o $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 name
of an input should not be generated dynamically. Instead use a constant. The dynamic value of that input should be set in the value
attribute of the input.
Example for one of your inputs:
<input type="text" name="phone" value="<?php echo $phoneCount ?>" />
You can retrieve the value by using the $_GET or $_POST arrays. Assuming your form uses the POST method you can do the following:
$phone = $_POST['phone']; // the entered text for the phone field
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:
<input type="text" name="phone[]" value="<?php echo $phoneCount ?>" />
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:
<input type="hidden" name="id[]" value="<?php echo $id ?>" />
Or you use the id as associative array key for all other input fields:
<input type="text" name="phone[<?php echo $id ?>]" value="<?php echo $phoneCount ?>" />
You have to create a form for the page to submit the user changes back to the PHP, and make your PHP page accept a form input (POST), then parse that input, and use that data to update your database.
@darkie
As you've to retrieve all the records from a table and need to update them, you might have to use a loop for every record to update using PHP.
How to get the values?
- Use a counter, and give names to the input fields like 'phone1' and 'rating1' and so on
- Enclose all the <input> with form tag.
- use OnSubmit() action, and retrieve all the values and create a matching string through JS or JQuery. Eg: phone1:123 rank1:1 phone2:456 rank2:2 , Make a string like : 123::1|456::2 etc
- Send the string to your PHP backend - Split the string and start the loop
- With your primary key, use UPDATE to update them.
P.S :- I believe you should have echoed phoneCount and ratingCoung in the "value" attribute.
Note: name
should be static, you should store the value in value
attribute, otherwise how did u get the value of that input fields??
otherwise there is alternative is
use
USE extract($_POST)
so that you will get associative array with key of name
field and value of value
field
It is not a good practice to use dynamic names. That being said; it can be done.
I would prefix the names with "Phone_"
<input type="text" name="Phone_<?php echo $phoneCount ?>">
You can now loop all the Post-variables and pick out everyone that starts with "Phone_"
You loop like this:
foreach($_POST as $name => $value) {
print "$name : $value<br>";
}
精彩评论