从留言簿中删除邮件

时间:2015-06-09 09:51:02

标签: javascript php mysql

我正在尝试使用php开发一个简单的留言板。我已经实现了所有功能,现在我想添加一个删除注释的功能,但我不知道该怎么做。 我想允许用户仅使用复选框或其他内容删除他的评论

Home.php(这是我在表格中添加评论的地方)

<?php
error_reporting(1);
ini_set('display_errors', 1);
?>

<?php


session_start();

if(!isset($_SESSION['login'])) {
    header("Location: Login.php");
    unset($_REQUEST);
}
else echo "<span class=\"welcome\"><strong>Benvenuto</strong> <em>" . $_SESSION['username'] . "</em></span>";
?>

<html>
<head>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src='jquery-1.11.3.js'></script>
    <script src='Script.js'></script>
</head>


<body>

<div id="functions">
    <form method=post id="addForm" name="addForm" action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>>
        <span class="label" style="text-align: center"> Comment </span> <textarea class="text" type=text cols=55 rows="7" name="comment"></textarea><br>
        <br><input class="submit" type=submit name=submit value="Submit">
    </form>
    <a href=viewGuest.php>View Guestbook!</a>

</div>


<div style="position:absolute; bottom:10px; left:50%; font-size: 15pt"><em>...</em> <a href="Logout.php">Logout</a></div>
</body>
</html>


<?php
if(isset($_POST['submit'])) {
    $name = $_SESSION['username'];
    $datetime = date("d-m-y h:i:s"); //date time
    $comment = $_POST["comment"];

    include("Database.php");

    $Database = new Database("localhost", "root", "1234");
    $Database->connectToServer();
    $Database->connectToDatabase("test");

    $result = $Database->insertComment($name, $comment, $datetime);

    if ($result) {
        echo "<br>";
        echo "☑ Successfull!";
        echo "<br>";
        echo "<a href=viewGuest.php>View Guestbook!</a>";
    } else echo "☒ Error, not inserted";
    unset($_REQUEST);

}
?>

viewGuest.php(这是我用表格显示所有消息的地方)

<head>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src='jquery-1.11.3.js'></script>
    <script src='Script.js'></script>
</head>

<body>
<?php

session_start();

if(!isset($_SESSION['login'])) {
    header("Location: Login.php");
    unset($_REQUEST);
} else $name = $_SESSION['username'];

include ("Database.php");

$Database = new Database( "localhost", "root", "1234");
$Database->connectToServer();
$Database->connectToDatabase("test");

$result = $Database->selectQuery("guestbook", "*");

echo "<div class=\"guestDiv\">";
echo "<table class=\"guestbook\" cellpadding='10'>";


echo "<th>Comment</th>";
while($rows=mysql_fetch_array($result)) {
    ?>
    <tr>
        <td width="10%"><?php echo $rows['name']; ?></td>
        <td width="20%"><?php echo $rows['datetime']; ?></td>
        <td width="70%"><?php echo $rows['comment']; ?></td>
    </tr>


<?php
}
echo "</table >";
?>

所有帖子都存储在

的数据库中

id(自动增量)

名称(提交者的用户)

datetime(日期格式为d-m-y h:i:s)

评论(评论本身)

1 个答案:

答案 0 :(得分:1)

感谢@RobSchmuecker的建议。现在我找到了解决方案

在viewGuest.php中,我修改了表格部分

while($rows=mysql_fetch_array($result)) {
    ?>
    <tr>
        <td width="10%"><?php echo $rows['name']; ?></td>
        <td width="20%"><?php echo $rows['datetime']; ?></td>
        <td width="60%"><?php echo $rows['comment']; ?></td>
        <td width="10%"><?php
            if($rows['name']== $name){
                echo "<a href=\"deletecomment.php?comment_id=" .  $rows['id'] ."\">Delete it</a>";
            }
            ?>
        </td>
    </tr>


<?php
}
echo "</table >";
?>

所以,我验证提交帖子的用户($ rows ['name'])是否与记录的用户(会话中设置的用户)相同,如果是,我在最后一列中添加了一个链接deletecomment.php和我传递带有id的变量,然后我将其删除

<?php
session_start();

if(!isset($_SESSION['login'])) {
    header("Location: Login.php");
    unset($_REQUEST);
}
else {
    if (isset($_GET['comment_id'])) {
        $comment_id = $_GET['comment_id'];

        include("Database.php");

        $Database = new Database("localhost", "root", "1234");
        $Database->connectToServer();
        $Database->connectToDatabase("test");

        $Database->deleteComment($comment_id);

        header("Location: viewGuest.php");

    }
}
?>