问题
1.fetch user data and display in tabular fashion
<?php
while($row=mysqli_fetch_assoc($rResultSet))
{
?>
<td><?php echo $row['id']?></td>
<td><?php echo $row['name']?></td>
<td><?php echo $row['user_role']?></td>
<td><?php echo $row['start_date']?></td>
<td>
<form id='euser' action='edituser.php' method='post'>
<input type="hidden" name="id" value="<?php echo $row['id']?>">
<input type="hidden" name="type" value="edituser">
<a href="javascript: document.getElementById('euser').submit();">Edit</i></a>|
</form>
<?php
}
?>
result of this code
2.when click on Edit link every time first id which 16 throw by form
i need , when press link edit at id 17 then id 17 thrown by form
回答1:
Try below if it help :
<?php
while($row=mysqli_fetch_assoc($rResultSet))
{
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['user_role'];?></td>
<td><?php echo $row['start_date'];?></td>
<td>
<form id='<?php echo $row['id'];?>' action='edituser.php' method='post'>
<input type="hidden" name="id" value="<?php echo $row['id'];?>">
<input type="hidden" name="type" value="edituser">
<a href="javascript: document.getElementById('<?php echo $row['id'];?>').submit();">Edit</a>|
</form>
</td>
</tr>
<?php
}
?>
回答2:
The problem is because of form's id
attribute, it has to be unique for each row. Right now you're assigning the same id value euser
for each row. For maintaining the uniqueness of id
value, what you can do is, append the $row['id']
with euser
for each table row. Also, you forgot to encapsulate each table row in <tr>...</tr>
. So your code should be like this:
<?php
while($row=mysqli_fetch_assoc($rResultSet)){
?>
<tr>
<td><?php echo $row['id']?></td>
<td><?php echo $row['name']?></td>
<td><?php echo $row['user_role']?></td>
<td><?php echo $row['start_date']?></td>
<td>
<form id='euser<?php echo $row['id']?>' action='edituser.php' method='post'>
<input type="hidden" name="id" value="<?php echo $row['id']?>">
<input type="hidden" name="type" value="edituser">
<a href="javascript: document.getElementById('euser<?php echo $row['id']?>').submit();">Edit</i></a>|
</form>
<tr>
<?php
}
?>
来源:https://stackoverflow.com/questions/44983764/while-loop-result-at-table-sends-first-row-when-submit-form-in-php