gpt4 book ai didi

php - SQL 的删除按钮

转载 作者:行者123 更新时间:2023-11-29 21:08:55 28 4
gpt4 key购买 nike

我有一个成员(member)/帐户网站,可以发布“注释”并将其保存在您的帐户中。当有人发布注释时,每个注释都会有一个编辑和删除按钮。我希望删除按钮删除相应的帖子,但是它会删除顶部的注释。我认为问题在于按下删除按钮时的其他时间或创建按钮时的时间。我不需要一种为按钮分配 id 的方法吗?

提前致谢。

<!-- connections.php connects to the database -->
<?php require 'connections.php'; ?>

<!-- check to make sure the user is logged in,
if not then redirect them to login.php -->
<?php session_start();
if(isset($_SESSION["UserID"])){
} else {
header('Location: Login.php');
die();
}?>

<!-- $result is a query containing all the notes
of the current user -->
<?php $UserID = $_SESSION["UserID"];
$result = mysqli_query($con, "SELECT * FROM notes WHERE UserID = '$UserID'");
?>

<!-- when you click 'save' upload the new note
to the database and refresh the page.
when you click 'delete' remote the note
that goes with that button from the database -->
<?php if(isset($_POST['save'])) {

session_start();
$note = $_POST['note'];
$UserID = ($_SESSION["UserID"]);

$sql = $con->query("INSERT INTO notes (UserID, note)Values('{$UserID}','{$note}')");

header('Location: Account.php');

} else if (isset($_POST['delete'])){
$result = mysqli_query($con, "SELECT * FROM notes WHERE UserID = '$UserID'");
$row = mysqli_fetch_array($result);
$noteID = $row['noteID'];
$UserID = ($_SESSION["UserID"]);

$sql = $con->query("DELETE FROM notes WHERE noteID = '$noteID'");

header('Location: Account.php');

} else if (isset($_POST['edit'])){


}?>

<!DOCTYPE HTML>
<html lang="en">
<head>
<title>My Account</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>

<body>

<h1 class="titleAct">Welcome</h1>

<form action="" method="post" name="notesubmit" id="notesubmit">

<div>
<textarea name="note" cols="50" rows="4" form="notesubmit" id="noteinput">New Note
</textarea>
</div>

<input name="save" type="submit" class="button" id="save" value="Save">

<!-- Whenever a note is saved, print out the
note with timestamp followed by the edit
and delete buttons for each note -->
<?php while ($row = mysqli_fetch_array($result)): ?>
<?php $note = $row['note'];?>
<?php $date = $row['time'];?>

<div id="note">
<p class="note"><?php echo $date; ?></br> ---------------- </br><?php echo $note; ?></p>
</div>

<input name="edit" type="submit" class="button" id="edit" value="Edit">
<input name="delete" type="submit" class="button" id="delete" value="Delete">

<?php endwhile; ?>
</form>

<div>
<a class="link" href="Logout.php">Logout</a>
<div>
<a class="link" href="Deactivate.php">Deactivate My Account</a>
</div>
</div>

</body>
</html>

最佳答案

你有一个逻辑错误...你没有发回你想要删除的笔记的 ID,你只是说你想要删除某些东西,然后从数据库,并删除第一个弹出的...

我要做的是将删除按钮更改为链接,并设置一个查询参数($_GET['noteID'])来选择要删除的 ID...

然后我会从数据库中选择 NoteID,但也要确保用户尝试删除自己的帖子,而不是其他人的帖子...

祝你好运:)

关于php - SQL 的删除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36580692/

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