2

我需要一些有关 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
    }   
    ?>

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

警告:mysql_fetch_array():提供的参数不是第 40 行 /home/mirho663/www-pub/webbpage/menu2.php 中的有效 MySQL 结果资源

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

4

1 回答 1

3

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

<?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
    }   
    ?>
于 2012-08-02T15:33:17.250 回答