小型bbs论坛系统开发3 后台父板块展示/删除

本文介绍了如何使用PHP开发小型BBS论坛系统后台的父版块展示和删除功能。学习过程中,作者深入理解了$_GET变量的运用,并通过实际操作掌握了SERVER['REQUEST_URI']的用法。项目包含多个文件,如header.inc.php、footer.inc.php、father.module.php、father.module.delete.php等,涉及到数据库操作、页面跳转函数和样式引用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PS:因为需要用到的html模板页面比较多,但目前学习重心是php,所以将html模板直接拷过来用了。看了一下布局的设计,如果让博主来做,是完全没有问题的。所以就直接拷贝过来,直接写php相关的操作了。


通过这一章的学习,学到了很多比较‘毒’的操作。
比如关于 SERVER[REQUSEURI] _GET赋值
然后在后一个页面用a链接直接调用去到他的相对路径等等。
大部分还是学习了关于$_GET的用法吧,十分受益!

这个就是后台页面的模板~直接拿来用啦。
这里写图片描述


项目布局:
admin
|–style
|–inc
|—-header.inc.php
|—-footer.inc.php
inc
|–mysql.inc.php
|–config.inc.php
|–tool.inc.php
father.module.php
father.module.delete.php
confirm.php


1>首先创建相关的数据库

//创建数据库:
mysql> create database sfkbbs default character set utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.02 sec)

//使用数据库:
mysql> use sfkbbs;
Database changed

//创建父模块表:
mysql> create table sfk_father_module(
    -> id int unsigned auto_increment primary key,
    -> module_name varchar(32) NOT NULL,
    -> sort int default 0
    -> );
Query OK, 0 rows affected (0.04 sec)

//表结构如下:
mysql> describe sfk_father_module;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| module_name | varchar(32)      | NO   |     | NULL    |                |
| sort        | int(11)          | YES  |     | 0       |                |
+-------------+------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

2>创建父模块信息文件 father.module.php:

<?php 
include_once '../inc/config.inc.php';
include_once '../inc/mysql.inc.php';
$link = sql_connect();

?>

<?php include_once './inc/header.inc.php'; ?>
    <div id="main" style="height:1000px;">
        <div class="title">父板块信息</div>
        <table class="list">
            <tr>
                <th>排序</th>         
                <th>版块名称</th>
                <th>操作</th>
            </tr>

            <?php 
            $query ="select * from sfk_father_module";
            $result = sql_execute($link,$query);
            while($data = mysqli_fetch_assoc($result)){
                // 实际删除代码:
                // father.module.delete.php?id={$data['id']}
                $url =urlencode("father.module.delete.php?id={$data['id']}");
                $returnUrl = urlencode($_SERVER['REQUEST_URI']);
                $deleteUrl = "confirm.php?url={$url}&returnUrl={$returnUrl}";
$html=<<<STRING
            <tr>
                <td><input class="sort" type="text" name="sort" /></td>
                <td>{$data['module_name']} id:[{$data['id']}]</td>
                <td>
                    <a href="#">[访问]</a>&nbsp;&nbsp;
                    <a href="#">[编辑]</a>&nbsp;&nbsp;
                    <a href="{$deleteUrl}">[删除]</a>
                </td>
            </tr>
STRING;
            echo $html;
            }
            ?>


        </table>
    </div>  

<?php include_once './inc/footer.inc.php'; ?>

3>其中引用html样式的文件存放/admin/inc内:

header.inc.php:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title>后台界面</title>
<meta name="keywords" content="后台界面" />
<meta name="description" content="后台界面" />
<link rel="stylesheet" type="text/css" href="style/public.css" />
</head>
<body>
    <div id="top">
        <div class="logo">
            管理中心
        </div>
        <ul class="nav">
            <li><a href="http://www.sifangku.com" target="_blank">私房库</a></li>
            <li><a href="http://www.sifangku.com" target="_blank">私房库</a></li>
        </ul>
        <div class="login_info">
            <a href="#" style="color:#fff;">网站首页</a>&nbsp;|&nbsp;
            管理员: admin <a href="#">[注销]</a>
        </div>
    </div>
    <div id="sidebar">
        <ul>
            <li>
                <div class="small_title">系统</div>
                <ul class="child">
                    <li><a class="current" href="#">系统信息</a></li>
                    <li><a href="#">管理员</a></li>
                    <li><a href="#">添加管理员</a></li>
                    <li><a href="#">站点设置</a></li>
                </ul>
            </li>
            <li><!--  class="current" -->
                <div class="small_title">内容管理</div>
                <ul class="child">
                    <li><a class="current" href="#">父板块列表</a></li>
                    <li><a href="#">添加父板块</a></li>
                    <li><a href="#">子板块列表</a></li>
                    <li><a href="#">添加子板块</a></li>
                    <li><a href="#">帖子管理</a></li>
                </ul>
            </li>
            <li>
                <div class="small_title">用户管理</div>
                <ul class="child">
                    <li><a href="#">用户列表</a></li>
                </ul>
            </li>
        </ul>
    </div>

footer.inc.php:

</body>
</html>

4>父模块删除文件 father.module.delete.php:

<?php 
include_once '../inc/config.inc.php';
include_once '../inc/mysql.inc.php';
include_once '../inc/tool.inc.php';
if(!isset($_GET['id']) || !is_numeric($_GET['id'])){
    skip('father.module.php','error','id参数传递失败!');
}

$link = sql_connect();
$query = "delete from sfk_father_module where id = {$_GET['id']}";


sql_execute_bool($link,$query);
if(mysqli_affected_rows($link) ==1){
    skip('father.module.php','ok','删除成功!3秒后自动跳转.');
}else{
    skip('father.module.php','error','删除失败,请稍后重试!3秒后自动跳转.');
}

?>

5>确认页confirm.php:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title>确认页</title>
<link rel="stylesheet" type="text/css" href="style/remind.css" />
</head>
<body>
    <div class="notice">
        <span class="pic ask"></span>
        <span>确认要删除吗?!</span>
        <a href="<?php echo $_GET['url']?>">确认</a>|

        <!-- 此时$_SERVER['REQUEST_URI']获取的值变成a标签跳转的相对路径 -->
        <a href="<?php echo $_GET['returnUrl'];?>">取消</a>
    </div>
</body>
</html>

6>其中跳转函数写在了tool.inc.php

<?php 

/*
@$url:跳转后的地址
@$pic:图标,可选:ok,error,ask
@message:跳转时的信息
 */
function skip($url,$pic,$message){
$html = <<<HTML
            <!DOCTYPE html>
            <html lang="zh-CN">
            <head>
            <meta charset="utf-8" />
            <meta http-equiv="refresh" content="3;URL={$url}">
            <title>跳转页</title>
            <link rel="stylesheet" type="text/css" href="style/remind.css" />
            </head>
            <body>
            <div class="notice">
                <span class="pic {$pic}"></span>
                {$message}<br/>
                <a href = "{$url}">如果响应时间过长,请点击该链接直接跳转。</a>
            </div>
            </body>
            </html>
HTML;
            echo $html;
            exit;
}

 ?>

7>当然,还有mysql数据库连接文件,及数据库函数库。
config.inc.php:

<?php 
//数据库配置文件。

header("Content-Type:text/html;charset=utf-8");
define('DB_HOST', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'root');
define('DB_NAME', 'sfkbbs');
define('DB_PORT', 3306);

?>

mysql.inc.php:

<?php 
include_once 'config.inc.php';

//数据库连接
function sql_connect($host=DB_HOST,$user=DB_USERNAME,$pass=DB_PASSWORD,$db_name=DB_NAME,$port=DB_PORT){
    $link = mysqli_connect($host,$user,$pass,$db_name,$port);
    if(mysqli_connect_errno()){
        exit(mysqli_connect_error());
    }
    mysqli_set_charset($link,'utf8');
    return $link;
}


//执行一条SQL语句,返回结果集对象或者返回布尔值
function sql_execute($link,$query){
    $result = mysqli_query($link,$query);
    if(mysqli_errno($link)){
        exit(mysqli_error($link));
    }
    return $result;
}



//执行一条SQL语句,只会返回布尔值
function sql_execute_bool($link,$query){
    $bool = mysqli_real_query($link,$query);
    if(mysqli_errno($link)){
        exit(mysqli_error($link));
    }
    return $bool;
}



//一次性执行多条SQL语句
/*
@$link:连接
@$arr_sqls:数组形式的多条sql语句
@$error:错误信息
*/
function sql_execute_multi($link,$arr_sqls,&$error){
    $sqls=implode(';',$arr_sqls).';';
    if(mysqli_multi_query($link,$sqls)){
        $data=array();
        $i=0;//计数
        do {
            if($result=mysqli_store_result($link)){
                $data[$i]=mysqli_fetch_all($result);
                mysqli_free_result($result);
            }else{
                $data[$i]=null;
            }
            $i++;
            if(!mysqli_more_results($link)) break;
        }while (mysqli_next_result($link));
        if($i==count($arr_sqls)){
            return $data;
        }else{
            $error="sql语句执行失败:<br />&nbsp;数组下标为{$i}的语句:{$arr_sqls[$i]}执行错误<br />&nbsp;错误原因:".mysqli_error($link);
            return false;
        }
    }else{
        $error='执行失败!请检查首条语句是否正确!<br />可能的错误原因:'.mysqli_error($link);
        return false;
    }
}

//获取记录数
function sql_num($link,$sql_count){
    $result=sql_execute($link,$sql_count);
    $count=mysqli_fetch_row($result);
    return $count[0];
}

//数据入库之前进行转义,确保,数据能够顺利的入库
function sql_escape($link,$data){
    //判断$data数据类型是否为字符串
    if(is_string($data)){
        $data = mysqli_real_escape_string($link,$data);
        return $data;
    }
    //判断$data数据类型是否为数组
    if(is_array($data)){
        foreach ($data as $key => $value) {
            $data[$key] = sql_escape($link,$value);
        }
        return $data;
    }
}



//关闭与数据库的连接
function sql_close($link){
    mysqli_close($link);
}

?>

这里写图片描述


这里写图片描述


这里写图片描述


这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值