0

我正在编写一个脚本,允许用户创建团队、向其他已注册的人发送合同等等。我希望登录的用户能够退出团队。html 表是使用 php 从 mysql 动态填充的。我可以让它将我的删除选项应用于其中的所有用户,<td></td>但不仅仅是登录的用户。这里有一些代码片段,希望能有所帮助。基本上我只希望登录的用户有删除选项。

    Id   Player
    1    User 1 - No Delete Option
    2    User 2 - Delete Option (Is the logged in user)
    3    User 3 - No Delete Option

    session_start();
    $get_users_name = $_SESSION['username_u'];

    $sql = ("SELECT id, username FROM user WHERE username='$get_users_name'");
    $result = mysql_query($sql) or die(mysql_error());

    while ($row = mysql_fetch_array($result)) {
    $grab_id = $row['id'];
    $grab_user = $row['username'];

//the rest of the table generation done here such as <tr> etc

    echo "<td>";
    if ($grab_user == $get_users_name) { 
    echo '<a href="user_delete.php?id='.$grab_id.'" onClick="return confirm(\'Are you sure you want to withdrawl from the team?\')">'.$grab_user.'</a>';
    } 
    else
    { 
    echo $grab_user;
    }
    echo "</td>";

//the rest of the table generation done here such as </tr> etc

}

**编辑以修复@easteregg 捕获的回声问题

4

1 回答 1

1

只要确保您将代码样式设置为可读性,然后您会注意到您的if条件在echo;)

试试这个它应该工作!

echo "<td>";

if ($grab_user == $get_users_name)  {
    echo '<a href="user_delete.php?id='.$grab_id.'" onClick="return confirm(\'Are you sure you want to withdrawl from the team?\')">'.$grab_user.'</a>';
} else {
    echo $grab_user;
}

echo "</td>";

请注意,如果用户有权删除某些内容,您应该再次检查 user_delete.php,否则您会遇到一些奇怪的麻烦;)

于 2012-07-26T06:57:44.617 回答