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

时间:2012-08-02 15:24:18

标签: php button posts spl

我需要一些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结果资源

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

1 个答案:

答案 0 :(得分: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
    }   
    ?>