美文网首页
2019-08-01 PHP+MySQL开发简单的留言板(4)

2019-08-01 PHP+MySQL开发简单的留言板(4)

作者: NoelleMu | 来源:发表于2019-08-01 16:49 被阅读0次

    现在我们需要开发后台管理页面和与之对应的删除留言、用户的功能。

    后台管理页面的开发

    后台管理页面需要显示所有的用户、留言信息,其本质是从数据库中查询信息之后循环输出到表格里。

    考虑到安全性(输入的内容中可能带有XSS),所以采用htmlspecialchars()函数将输出的内容转换为非HTML元素。

    admin.php

    <html>
    <head>
        <link rel="stylesheet" href="css/admin.css">
    </head>
    <body>
        <div class="topbar">
            <a href="index.php" class="indexbtn">首页</a>
            <a href="logout.php" class="loginOrLogoutLink">登出</a>
        </div>
        <div class="bigbox">
        <?php
        /**
         * 后台管理页面
         * @Author NullP0
         */
        session_start();
        error_reporting(0);
        include_once 'connect.php';
    
        // 如果不是管理员登录(登录的用户名不是admin),就没有权限访问此页面,于是跳转回主页
        if ($_SESSION['username'] !== 'admin') {
            die('<script>document.location.href = "index.php"</script>');
        }
    
        // 用户管理模块
        echo '<div class="title">用户管理</div>';
    
        mysqli_select_db($con, 'bbs');      // 选择数据库
        mysqli_set_charset($con, 'utf-8');   // 选择字符集
    
        $getCountSql = 'select count(id) as coun from user';
        $result = mysqli_query($con, $getCountSql);
        $data = mysqli_fetch_assoc($result);
        $count = $data['coun'];                     // 根据查询结果获得总用户数
    
        // 从数据库中查询用户信息
        $usrsql = 'select id, username, createtime, createip from user order by id';
        $result = mysqli_query($con, $usrsql);
        if ($result && mysqli_num_rows($result)) {
            // 将用户数据循环显示在表格里
            echo '<table class="mainTable" cellspacing="0">';
            echo '<tr>';
            echo '<td class="content">' . '用户名' . '</td>';
            echo '<td class="content">' . '注册时间' . '</td>';
            echo '<td class="content">' . 'ip' . '</td>';
            echo '<td class="content">' . '操作' . '</td>';
            echo '</tr>';
            while ($row = mysqli_fetch_assoc($result)) {
                echo '<tr>';
                echo '<td class="content">' . htmlspecialchars($row['username']) . '</td>';
                echo '<td class="content">' . date('Y-m-d H:i:s', $row['createtime']) . '</td>';
                echo '<td class="content">' . long2ip((int)$row['createip']) . '</td>';
                echo '<td class="content"><a href="deleteusr.php?id=' . $row['id'] . '">删除用户</a></td>';
                echo '</tr>';
            }
            echo '</table>';
        } else {
            echo '没有用户数据';
        }
    
    
        // 留言管理模块
        echo '<div class="title">留言管理</div>';
    
        mysqli_select_db($con, 'message');      // 选择数据库
        mysqli_set_charset($con, 'utf-8');   // 选择字符集
    
    
        $getMeaasgeSql = 'select count(id) as messageCount from user';
        $result = mysqli_query($con, $getMeaasgeSql);
        $data = mysqli_fetch_assoc($result);
        $messageCount = $data['messageCount'];                     // 根据查询结果获得总留言数
    
        // 从数据库中查询留言信息
        $msgsql = 'select id, user, title, content, time from message order by id';
        $result = mysqli_query($con, $msgsql);
        if ($result && mysqli_num_rows($result)) {
            // 将用户数据循环显示在表格里
            echo '<table class="mainTable" cellspacing="0">';
            echo '<tr>';
            echo '<td class="content">' . '用户名' . '</td>';
            echo '<td class="content">' . '标题' . '</td>';
            echo '<td class="content">' . '内容' . '</td>';
            echo '<td class="content">' . '添加时间' . '</td>';
            echo '<td class="content">' . '操作' . '</td>';
            echo '</tr>';
            while ($row = mysqli_fetch_assoc($result)) {
                echo '<tr>';
                echo '<td class="content">' . htmlspecialchars($row['user']) . '</td>';     // htmlspecialchars用来防止XSS
                echo '<td class="content">' . htmlspecialchars($row['title']) . '</td>';
                echo '<td class="content">' . htmlspecialchars($row['content']) . '</td>';
                echo '<td class="content">' . $row['time'] . '</td>';
                echo '<td class="content"><a href="deletemsg.php?id=' . $row['id'] . '">删除留言</a></td>';
                echo '</tr>';
            }
            echo '</table>';
        } else {
            echo '没有留言数据';
        }
    
        mysqli_close($con);
    
        ?>
        </div>
    </body>
    </html>
    
    

    admin.css

    * {
        margin: 0;
        padding: 0;
    }
    
    html {
        font-family: DengXian, "Microsoft YaHei";
        background-color: #dFdFdF;
    }
    
    a {
        color: #036;
    }
    
    .topbar {
        display: flex;
        width: 100%;
        height: 40px;
        background-color: #036;
        position: fixed;
    }
    
    .indexbtn {
        display: flex;
        align-items: center;
        justify-content: center;
        padding-left: 30px;
        padding-right: 30px;
        font-size: 1.2em;
        color: white;
        text-decoration: none;
    }
    
    .mainTable {
        display: flex;
        padding-top: 50px;
        align-items: center;
        justify-content: center;
    }
    .content {
        margin: 0;
        padding:10px 50px 10px 10px;
        border: 1px solid #036;
    }
    
    .title {
        font-size: 3em;
        font-weight: 800;
        text-align: center;
        margin-bottom: 20px;
        padding-bottom: 10px;
        padding-top: 100px;
        color: #036;
        border-bottom: 1px solid #003366;
    }
    
    .bigbox {
        padding: 0 100px 100px 100px;
        margin: 0 200px 100px 200px;
        background-color: white;
        box-shadow: 0 5px 10px grey;
    }
    
    .loginOrLogoutLink {
        display: flex;
        margin-left: auto;
        align-items: center;
        justify-content: center;
        padding-left: 30px;
        padding-right: 30px;
        font-size: 1.2em;
        color: white;
        text-decoration: none;
    }
    

    实际效果:

    删除用户、留言功能的实现

    删除功能的实质就是从数据库中删除特定的记录。

    这里的设计思路是接受从admin.php传过来的id参数,并在数据库中删除对应id的记录。

    deletemsg.php

    <?php
        /**
        * 用户删除页面
        * @Author NullP0
        */
        session_start();
    
        include_once 'connect.php';
    
        if ($_SESSION['username'] !== 'admin') {
            die('<script>document.location.href = "index.php"</script>');
        }
    
        mysqli_select_db($con, 'message');      // 选择数据库
        mysqli_set_charset($con, 'utf-8');   // 选择字符集
    
        // 判断数据的合法性
        if (is_numeric($_GET['id'])) {
            $id = (int) $_GET['id'];
        } else {
            die('数据不合法');
        }
    
        $sql = "delete from message where id in($id)";
    
        $result = mysqli_query($con, $sql);
    
        if ($result) {
            die('<script>alert("删除成功!"); document.location.href = "admin.php"</script>');
        } else {
            die('<script>alert("删除失败!"); document.location.href = "admin.php"</script>');
        }
    ?>
    

    deleteusr.php

    <?php
        /**
         * 用户删除页面
         * @Author NullP0
         */
        session_start();
    
        include_once 'connect.php';
    
        if ($_SESSION['username'] !== 'admin') {
            die('<script>document.location.href = "index.php"</script>');
        }
    
        mysqli_select_db($con, 'bbs');      // 选择数据库
        mysqli_set_charset($con, 'utf-8');   // 选择字符集
    
        // 判断数据的合法性
        if (is_numeric($_GET['id'])) {
            $id = (int) $_GET['id'];
        } else {
            die('数据不合法');
        }
    
        $sql = "delete from user where id in($id)";
    
        $result = mysqli_query($con, $sql);
    
        if ($result) {
            die('<script>alert("删除成功!"); document.location.href = "admin.php"</script>');
        } else {
            die('<script>alert("删除失败!"); document.location.href = "admin.php"</script>');
        }
    
    ?>
    

    相关文章

      网友评论

          本文标题:2019-08-01 PHP+MySQL开发简单的留言板(4)

          本文链接:https://www.haomeiwen.com/subject/llrcdctx.html