gpt4 book ai didi

php - 从用户表 PHP MYSQL 中删除行

转载 作者:可可西里 更新时间:2023-11-01 06:46:37 24 4
gpt4 key购买 nike

我想在用户单击按钮时从我的用户表中删除一行,用户需要登录才能删除自己的帐户。

我回显了显示“4”的 $user_id,这是已登录用户的正确 ID,因此 user_id = $user_id

这是我拥有的页面,其中包含我要删除数据库中用户行的按钮

<?php
include_once 'dbconfig.php';
if(!$user->is_loggedin())
{
$user->redirect('index.php');
}
$user_id = $_SESSION['user_session'];

if(isset($_POST['leave'])){
$stmt = $DB_con->prepare("DELETE FROM users WHERE user_id = $user_id ");
$stmt->execute();
}
$stmt = $DB_con->prepare("SELECT * FROM users WHERE user_id=:user_id");
$stmt->execute(array(":user_id"=>$user_id));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
?>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="style.css" type="text/css" />
<title>Welcome - <?php print($userRow['user_email']); ?></title>
</head>

<body>

<div class="header">

<div class="right">
<label><a href="logout.php?logout=true"><i class="glyphicon glyphicon-log-out"></i> logout</a></label>
</div>
</div>
<div class="content">

Welcome <?php print($userRow['user_name']); ?> <br>
<?php print($userRow['team_name']);?><br>
Rank <?php print($userRow['user_rank']); ?> <br>
<a href="players.php">Players</a>
<a href="teams.php">Teams</a>

<form action='teams.php' method='post'>
<input type='submit' name='leave' value='Delete Profile'/> </form>

<?php echo $user_id?>

</div>
</body>
</html>

最佳答案

我认为您的问题是您的表单操作 (teams.php) 将接收发布数据。您的删除代码位于同一文件中,逻辑上永远不会在此页面中设置 $_POST['leave']。

只需尝试删除表单操作属性中的 teams.php。

<form action='' method='post'>
<input type='submit' name='leave' value='Delete Profile'/> </form>

或者在你的 teams.php 文件中添加你的删除代码

//Make sure you have started the session before using it
$user_id = $_SESSION['user_session'];

if(isset($_POST['leave'])){
$stmt = $DB_con->prepare("DELETE FROM users WHERE user_id = $user_id ");
$stmt->execute();
}

另一条建议是使用参数化查询。 示例:

if(isset($_POST['leave'])){
$stmt = $DB_con->prepare("DELETE FROM users WHERE user_id = ? ");
$stmt-> bindParam(1,$user_id);
$stmt->execute();

}

关于php - 从用户表 PHP MYSQL 中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34259334/

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