gpt4 book ai didi

php - 只给登录用户删除选项

转载 作者:行者123 更新时间:2023-11-29 14:21:33 25 4
gpt4 key购买 nike

我正在编写一个脚本,允许用户创建团队、向注册的其他人发送契约(Contract)等等。我希望登录的用户能够退出团队。 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捕获的 echo 问题

最佳答案

只要确保您的代码具有可读性,那么您就会注意到 echo 中有 if 条件;)

试试这个应该可行!

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 中再次检查用户是否有权删除某些内容,否则您会遇到一些奇怪的麻烦;)

关于php - 只给登录用户删除选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11663800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com