我正在为用户创建的每个“注释”生成删除按钮。但是,无论您单击哪个删除,它都会删除最近保存的注释,而不是与注释对应的注释。我假设我的隐藏字段'deleteID'出了问题。
<!-- 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'])){
$deleteID = $_POST['deleteID'];
$sql = $con->query("DELETE FROM notes WHERE noteID = '$deleteID'");
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'];?>
<?php $noteID = $row['noteID'];?>
<div id="note">
<p class="note"><?php echo $date; ?></br> ---------------- </br><?php echo $note; ?> </p>
</div>
<input name="deleteID" type="hidden" id="hidden<?php echo $noteID;?>" value="<?php echo $noteID; ?>">
<input name="delete" type="submit" class="button" 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>
答案 0 :(得分:1)
我认为它根本不是删除 - 而是带有注释的div - 每个都有相同的id,因为它们在while循环中,因此每个note div的id都是“note”
<div id="note">
它应该是
<div id="note<?php echo $noteID;?>";
答案 1 :(得分:0)
错误似乎在输入的名称中。应与您在ID部分中设置的值相同。
像这样:
<input name="hidden<?php echo $noteID;?>" type="hidden" id="hidden<?php echo $noteID;?>" value="<?php echo $noteID; ?>">
否则,您的所有输入都具有相同的名称,并且最后一个输入执行查询。
修改强>
直接在提交按钮中输入要删除的值的名称,如下所示:
<input name="delete-<?php echo $noteID;?>" type="submit" class="button" value="Delete">
然后,您可以获取$_POST
数组中的值和explode()
-
分隔符上的值,以获取ID值。
答案 2 :(得分:0)
您还有一个表单的多个提交输入 - 一个用于触发保存,每个用于每个回显删除注释,因此触发删除提交功能将首先触发保存提交 - 因此$ _POST [' save']将在$ _POST ['delete']之前执行。这可能是改变最后一个输入音符而不是触发动作的动作背后的问题。
我建议将添加注释功能拆分为与删除注释功能不同的形式。您可以使用相同的if else逻辑,这应该在新版本中按预期工作,因为您实际上是在发布您期望的信息。