这比标题可能让您相信的要复杂一些。我有一个功能,可以在用户进入页面后立即查询我的数据库。然后查询返回 id
和 note_name
(这些是我数据库中列中的名称)。我可以让笔记显示得很好,但我遇到的问题是删除它们。我对如何动态地告诉服务器当前正在选择哪个音符感到非常困惑。
如果我提供了足够的信息来解决这个问题,请告诉我,我将删除不必要的代码以节省空间。所有代码都在同一个 .php
脚本中。
笔记.php
select_notes() 函数
// query DB for pre-existing notes
function select_notes() {
include('includes/connection.php');
$username = $_SESSION['username'];
// make READ query to the db and return the results
$query = "SELECT `id`, `note_name` FROM `notes` WHERE `username`='$username'";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// create empty array for future use
$note_id = [];
$note_name = [];
while ($row = mysqli_fetch_assoc($result)) {
// push all the data from $row into $note_name array
array_push($note_id, $row['id']);
array_push($note_name, $row['note_name']);
}
// close connection and return array containing note details from DB
mysqli_close($conn);
return [ $note_id, $note_name ];
} else {
mysqli_close($conn);
echo '<p>No notes available</p>';
}
}
在 HTML 中调用 select_notes()
的地方
<div class="saved-notes col-10 offset-1 col-md-4 offset-md-0">
<header class="text-center">
<h2>Saved Notes</h2>
</header>
<div class="pt-2">
<form action="<?php htmlspecialchars($_SERVER['PHP_SELF']); ?>" class="form-inline px-1" method="POST">
<input class="form-control mb-1 mr-1" name="note_name" type="text">
<input class="btn btn-success btn-sm" name="create_note" type="submit" value="Save">
</form>
<?php
if ($_POST['create_note']) {
insert_note();
}
list($note_id, $note_name) = select_notes();
foreach (select_notes() as $note) {
foreach ($note as $value) {
echo '
<form action="notes.php" class="single-note-form" method="POST">
<button name="edit_note" type="submit">'.$value.'</button>
<button class="text-danger" name="delete_note" type="submit">×</button>
</form>
';
}
}
?>
</div>
</div><!-- col -->
最佳答案
在您的删除表单中,您必须嵌入您在查询中选择的笔记的 ID
<form action="notes.php" class="single-note-form" method="POST">
<button name="edit_note" type="submit">'.$value.'</button>
<button class="text-danger" name="delete_note" type="submit">×</button>
//include a hidden field containing the records ID
<input type="hidden" name="note_id" value="?" />
</form>
您会发现返回数组的结构受到限制。目前你有 2 个数组,一个是注释文本(我假设),另一个是注释 id。我会像这样将其组合成一行(在 select_notes 内):
$notes = [];
while ($row = mysqli_fetch_assoc($result)){
$notes[] = ['id' => $row['id'], 'note_name' => $row['note_name']];
}
return $notes;
这样您将拥有一个多维数组,很像数据库返回的内容,然后在您的 HTML 中:
foreach (select_notes() as $note) {
echo '
<form action="notes.php" class="single-note-form" method="POST">
<button name="edit_note" type="submit">'.$note['note_name'].'</button>
<button class="text-danger" name="delete_note" type="submit">×</button>
<input type="hidden" name="note_id" value="'.$note['id'].'" />
</form>';
}
或者通过其他方式将 note_id
放入表单中。
如果您需要一个 ID 列表,使用 $ids = array_column($notes, 'id')
将它从多维数组中拉出来非常简单,因此您不会有任何损失通过使用组合结构。事实上,它使代码更清晰、更短、更简洁。
http://php.net/manual/en/function.array-column.php
但是无论如何,一旦它被嵌入到表单中,它就变得简单了,因为它与 post 请求一起传递。然后,当您执行删除操作时,只需 $_POST['note_id']
即可。
您还应该在查询中使用准备好的语句,即使它是 session 数据并且您认为它是干净的。谁知道线下会发生什么变化,您可能会引入一种方法让用户修改该值,然后就是开放季节。应该始终进行 SQL 注入(inject)预防。只需很少的努力即可完成,然后您就会放心地知道您的代码是好的。
最后一件事:
$query = "SELECT `id`, `note_name` FROM `notes` WHERE `username`='$username'";
我会说使用 username
可能会在某些时候让您感到悲伤。这实际上应该使用用户表中的 id
。现在您的用户名可能是唯一的,但从纯粹的性能角度来看,使用数字索引比字符串快得多。而且,好吧,这让我有点烦...
关于PHP/MySQL : Deleting data from DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48159290/