gpt4 book ai didi

php - 每个帖子后的删除按钮(php,sql)

转载 作者:行者123 更新时间:2023-12-04 05:37:14 24 4
gpt4 key购买 nike

我需要一些 PHP 和 SQL 方面的帮助。我正在做一个网站,您可以在其中发布不同主题(工作、家庭、学校等)的笔记。在从我的数据库中选择每个笔记之后,我想要一个按钮,可以在不再需要时删除该特定帖子。我可以让它删除,但删除错误的笔记,总是上面或下面的。我不知道我的代码有什么问题?请帮帮我。

    <?php 
$query = "SELECT * FROM notes WHERE subject='Work' order by id desc";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$subject = $row['subject'];
$date = $row['date'];
$note = $row['note'];

print "<p><strong>$subject</strong> ($id), $date </p>";
print "<p> $note </p>";

?>
//delete button starts here here
<form id="delete" method="post" action="">
<input type="submit" name="delete" value="Delete!"/>
<?php
if(isset($_POST['delete'])){
$query = "DELETE FROM notes WHERE id=$id";
$result = mysql_query($query);
}
?>
</form>
<?php
}
?>

当我按删除时,我得到了这个:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mirho663/www-pub/webbpage/menu2.php on line 40



这是什么意思,我该如何解决?

最佳答案

我在下面更新了您的脚本,如果有效,请尝试。

<?php 

if(isset($_POST['delete'])){
$id = $_POST['delete_rec_id'];
$query = "DELETE FROM notes WHERE id=$id";
$result = mysql_query($query);
}

$query = "SELECT * FROM notes WHERE subject='Work' order by id desc";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$subject = $row['subject'];
$date = $row['date'];
$note = $row['note'];

print "<p><strong>$subject</strong> ($id), $date </p>";
print "<p> $note </p>";

?>
//delete button starts here here
<form id="delete" method="post" action="">
<input type="hidden" name="delete_rec_id" value="<?php print $id; ?>"/>
<input type="submit" name="delete" value="Delete!"/>

</form>
<?php
}
?>

关于php - 每个帖子后的删除按钮(php,sql),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11780800/

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