1

I am trying to pass the column values from a single row generated from a while loop. When the link is clicked I want certain values from that row to be passed as hidden fields to another page.

The code:

<form id="repeat" name="repeat" action="edit_user.php" method="post">
<?php
$counter = 0;
while($row = $result->fetch_array()) {
$color = ($counter & 1)? "#D7ECEC" : "#DEDEDE";
$counter++;
?>
<tr align="left" valign="middle" style="background: <?php print $color; ?>">
<td><a href="#" onclick="document.getElementById('repeat').submit();" >
    <?php echo $row['firstname']; ?> <?php echo $row['surname']; ?></a></td>
<td><?php echo $row['userlogin']; ?></td>
<td><?php echo $row['accesslvl']; ?></td>
<td ><?php echo $row['chgpasswrd']; ?></td>
</tr>
<input type="hidden" name="id" value="<?php echo $row['userID']; ?>" />
<input type="hidden" name="lvl" value="<?php echo $row['accesslvl']; ?>" />
<?php } ?>
</table>
</form>

What is happening is that the hidden fields are passing values for the last row of the while loop. Is it possible to do this so the hidden values being passed are from the same row of the link that is being clicked?

I've tried a few things and nothing works and I've not been able to find anything that directly relates to this problem. Any help would be appreciated. Cheers

4

1 回答 1

0

你真的需要把它分成两页。第一个页面显示每个唯一用户的用户信息和链接,第二个页面提供用于发布表单数据的编辑表单,如下所示...

<table>
<?php
$counter = 0;
while($row = $result->fetch_array()) {
    $color = ($counter & 1)? "#D7ECEC" : "#DEDEDE";
    $counter++;
?>
<tr align="left" valign="middle" style="background: <?php print $color; ?>">
    <td>
        <a href="edit_user.php?userID=<?php echo $row['userID']; ?>&accesslvl=<?php echo $row['accesslvl']; ?>">
            <?php echo $row['firstname']; ?> <?php echo $row['surname']; ?>
        </a>
    </td>
    <td><?php echo $row['userlogin']; ?></td>
    <td><?php echo $row['accesslvl']; ?></td>
    <td ><?php echo $row['chgpasswrd']; ?></td>
</tr>
<?php } ?>
</table>

在 edit_user 页面上,您可以使用 url 查询字符串传入的信息填充隐藏字段。记得清理这些值以防止 sql 注入或 xss 攻击。我在这里使用了 htmlentities 作为示例,您可能希望对此进行增强,请阅读 htmlentites 上的 php 文档页面以获取更多信息 (ENT_QUOTES)。

$userID    = htmlentities($_GET['userID']);
$accesslvl = htmlentities($_GET['accesslvl']);

<form action="modify_user.php" method="post">
    <input type="hidden" name="id" value="<?php echo (int)$userID; ?>" />
    <input type="hidden" name="lvl" value="<?php echo $accesslvl; ?>" />

    //rest of form html
于 2013-05-29T07:53:47.863 回答