从数据库php中删除评论

时间:2015-04-19 17:16:39

标签: php mysql comments

我正在尝试从我的评论系统中删除评论,但我的代码无效。我希望能够从我的表中删除每条评论,包括父评论。目前,我的代码没有从删除按钮链接获取评论的参数。

function.php

<?php
function getComments($row) {
    echo "<li class='comment'>";
    echo "<div class='aut'>" . $row['author'] . "</div>";
    echo "<div class='comment-body'>" . $row['comment'] . "</div>";
    echo "<div class='timestamp'>" . $row['created_at'] . "</div>";
    echo "<a href='#comment_form' class='reply' id='" . $row['id'] . "'>Edit Song</a>"; 
    echo "<span> </span>";
    echo "<a href='delete.php?id='" . $row['id'] . "'>Delete Song</a>"; 
    $q = "SELECT * FROM threaded_comments WHERE parent_id = ".$row['id']."";
    $r = mysql_query($q);
    if(mysql_num_rows($r)>0)
        {
        echo "<ul>";
        while($row = mysql_fetch_assoc($r)) {
            getComments($row);
        }
        echo "</ul>";
        }
    echo "</li>";
}
?>

delete.php

<?php 
    include 'includes/config.php';

    $deleteid = $_GET['id'];

    mysql_query("DELETE FROM threaded_comments WHERE id='$deleteid'");

    echo "Song has been deleted!";
    header ('Location: index.php');
?>

我添加了index.php和post_comment页面。此外,删除功能在我进行更改时仍然无法正常工作。

index.php

<?php 
include("config.php");
include("functions.php");
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Multi-Lyric Collaboration System</title>
<script type='text/javascript' src='jquery.pack.js'></script>
<script type='text/javascript'>
$(function(){
    $("a.reply").click(function() {
        var id = $(this).attr("id");
        $("#parent_id").attr("value", id);
        $("#name").focus();
    });
});
</script>
<style type='text/css'>
html, body, div, h1, h2, h3, h4, h5, h6, ul, ol, dl, li, dt, dd, p, blockquote,
pre, form, fieldset, table, th, td { margin: 0; padding: 0; }

body {
font-size: 14px;
line-height:1.3em;
background: url('music.jpg');
height: 100%;
background-size:100% ;
background-position: static;
background-color: purple;
min-height: 100%;
background-repeat: no-repeat;
}
label{
    color: white;
    font-size: 2em;
}

a, a:visited {
outline:none;
color:#7d5f1e;
}

.clear {
clear:both;
}

#wrapper {
    width:480px;
    margin:0px auto;
    padding:15px 0px;
}

.comment {
    padding:5px;
    border:2px solid black;
    border-width: 1px;
    margin-top:15px;
    list-style:none;
    background-color: white;
      opacity: 0.96;
        -moz-border-radius:20px;
  -webkit-border-radius:20px;
  overflow-y: scroll;

}

.aut {
    font-weight:bold;
}

.timestamp {
    font-size:85%;
    float:right;
}

#comment_form {
    margin-top:15px;

}

#comment_form input {
    font-size:1.2em;
    margin:0 0 10px;
    padding:3px;
    display:block;
    width:100%;

}

#comment_body {
    display:block;
    width:100%;
    height:150px;

}

#submit_button {
    text-align:center; 
    clear:both;
}
header{
    color: white;
    text-align: center;
}
</style>
</head>
<body>

<div id='wrapper'>
    <header><font size="20">MultiLyric Collaborator</font></header>
    <br>
<ul>
<?php
$q = "SELECT * FROM threaded_comments WHERE parent_id = 0";
$r = mysql_query($q);
while($row = mysql_fetch_assoc($r)):
    getComments($row);
endwhile;
?>
</ul>

    <form id="comment_form" action="post_comment.php" method='post'>
    <label for="name">Name:</label>
    <input type="text" name="name" id='name'/>

    <label for="comment_body">Enter Song:</label>
    <textarea name="comment_body" id='comment_body'></textarea>
    <input type='hidden' name='parent_id' id='parent_id' value='0'/>
    <div id='submit_button'>
        <input type="submit" value="Add comment"/>
    </div>
</form>
</div>
</body>
</html>

post_comment.php

<?php
include("config.php");
$author = mysql_real_escape_string($_POST['name']);
$comment_body = mysql_real_escape_string($_POST['comment_body']);
$parent_id = mysql_real_escape_string($_POST['parent_id']);
$q = "INSERT INTO threaded_comments (author, comment, parent_id) VALUES ('$author', '$comment_body', $parent_id)";
$r = mysql_query($q) or die(mysql_error());
if(mysql_affected_rows()==1) {
    header("location:index.php");
}
else {
    echo "Comment cannot be posted. Please try again.";
}
?>

1 个答案:

答案 0 :(得分:0)

来自&#34; SELECT&#34; getComments()中的语句,看起来你需要做的是:

mysql_query("DELETE FROM threaded_comments WHERE parent_id ='$deleteid'");

这是&#39; parent_id&#39;不是&#39; id&#39;。