天天看点

从留言板开始做网站(十)——后台留言处理(数据删除)

我们先创建一个新的PHP页面,并命名为admin.php,我复制了首页的头部内容:

<!DOCTYPE html>
<?php
require "conn.php";

//设置输出编码
$conn->query("set names utf8");

//读取数据库
$sql = "select * from message order by id desc";
$result = $conn->query($sql);
 ?>
<html >
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=Edge">
        <meta charset="UTF-8">
        <title>留言板</title>
        <link rel="stylesheet" href="style/style.css" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >
        <!--[if lt IE 9]>
        <script src="http://cdn.bootcss.com/html5shiv/r29/html5.js"></script>
        <![endif]-->
    </head>
    <body>

    </body>
</html>
           

完整的HTML代码:

<pre name="code" class="php"><!DOCTYPE html>
<?php
//数据库连接
require "conn.php";

//设置输出编码
$conn->query("set names utf8");

//读取数据库
$sql = "select * from message order by id desc";
$result = $conn->query($sql);
 ?>
<html >
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=Edge">
        <meta charset="UTF-8">
        <title>留言板</title>
        <link rel="stylesheet" href="style/style.css" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >
        <!--[if lt IE 9]>
        <script src="http://cdn.bootcss.com/html5shiv/r29/html5.js"></script>
        <![endif]-->
    </head>
    <body>
        <h1>留言板后台</h1>
        <section class="wrap">
            <table>
                <thead>
                    <tr>
                        <th>编号</th>
                        <th>留言内容</th>
                        <th>用户名</th>
                        <th>留言时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    if ($result->num_rows > 0) {
                    //逐行输出数据
                        while ($row = $result->fetch_assoc()) {
                            echo "<tr><td>" . $row["id"] . "</td><td>" . $row["user_mes"] . "</td>
                            <td>" . $row["user_name"] . "</td><td>" . $row["send_time"] . "</td><td>
                            删除</td>";
                        }
                    }else {
                        echo "暂无留言";
                    }
                     ?>
                </tbody>
            </table>
        </section>
    </body>
</html>
           

其实跟首页显示留言是一样一样滴,再看下CSS代码:

/*留言板后台*/
h1 { text-align: center; }
table,td,th {
    border: 1px solid #666;
    border-collapse: collapse;
    text-align: center;
}
table {
    margin: 1.5em auto;
    width: 80%;
}
th {
    padding: .3em;
    color: #fff;
    letter-spacing: .5em;
    background-color: #383838;
}
td {
    padding: .3em;
}
           

最终的显示效果:

从留言板开始做网站(十)——后台留言处理(数据删除)

我们先搞删除的功能,先新建一个delete.php文件,用来存放处理数据删除的代码,并写入相应的代码:

<?php
header("Content-Type:text/html;charset=utf-8");
//数据库连接
require "conn.php";

//删除记录
$id = $_GET["id"];
$sql = "DELETE from message where id=" . $id;
if ($conn->query($sql)) {
    header("refresh:3;url=admin.php");
    echo "删除成功,正在返回留言列表";
}else {
    echo "删除失败";
}

//关闭数据连接
$conn->close();
?>
           

对了,我们将admin.php中的”删除“更改为一个链接:

<?php
if ($result->num_rows > 0) {
//逐行输出数据
    while ($row = $result->fetch_assoc()) {
        echo "<tr><td>" . $row["id"] . "</td><td>" . $row["user_mes"] . "</td><td>" . 
        $row["user_name"] . "</td><td>" . $row["send_time"] . "</td><td><a href='delete.php?id=" . 
        $row["id"] ."'>删除</a></td>";
    }
}else {
    echo "暂无留言";
}
 ?>
           

我们可以看到这段代码:

"</td><td><a href='delete.php?id=" .  $row["id"] ."'>删除</a></td>";
           

链接后面跟了问号,问号后面的参数可以用$_GET方法来获得,因此在delete.php中,我们就有:

$id = $_GET["id"];
           

然后根据where id 来告诉程序将对等ID的记录从数据表中删除。

$sql = "DELETE from message where id=" . $id;
           

我们在删除前应该要跳出一个提示框,以防误删,所以添加一个这样的功能。

先添加一个挂钩ID

"<td><a id='del' href='delete.php?id=" . $row["id"] ."'>删除</a></td>"
           

注意双引号内用单引号,单引号内用双引号。

然后写JS代码,这种小程序放在admin.php底部即可,不用另外导入JS文件。

var del = document.getElementById("del");
      del.οnclick=function(){
        var chose = confirm("确定删除这条记录吗?");
        if (!chose) return false;
      }
           
从留言板开始做网站(十)——后台留言处理(数据删除)