6 分类页增删改

 

本节知识点:向其它页面传递多个参数

urlencode($str);//编码 URL 字符串
此函数便于将字符串编码并将其用于 URL 的请求部分,同时它还便于将变量传递给下一页。

urldecode($str);//解码已编码的 URL 字符串
$_GET$_REQUEST 已经被解码了,不需要再使用该函数手动解码

 

get post 传递的来得数值都是字符串而非整数

 

mysqli_affected_rows($link);获取上一次mysql操作中受影响的行数

  

 

1)一级分类列表页 cate_index.php

<?php
include_once '../inc/config.inc.php';
include_once '../inc/mysql.inc.php';
$link=connect();
$template['title'] = '帖子分类页面';
$template['css'] = ['style/public.css','style/father_module_add.css'];
?>
<?php include 'inc/header.inc.php'?>
<div id="main">
    <div class="title">一级分类</div>
    <table class="list">
        <tr>
            <th>id</th>
            <th>排序值</th>
            <th>版块名称</th>
            <th>操作</th>
        </tr>
        <?php
        $query="select * from bbs_category order by sort desc";
        $result=execute($link,$query);
        while ($data=mysqli_fetch_assoc($result)){

            //1 创建删除url变量
            $del_url = "cate_del.php?id={$data['id']}";
            $del_url = urlencode($del_url);

            //2 创建返回本页的url变量
            $back_url = "{$_SERVER['REQUEST_URI']}";
            $back_url = urldecode($back_url);

            //3 需要传递的分类名称
            $msg = "{$data['name']}";

            //4 拼接参数
            $url = "confirm.php?url={$del_url}&back_url={$back_url}&msg={$msg}";
        ?>
            <tr>
                <td><?php echo $data['id'] ?></td>
                <td><?php echo $data['name'] ?></td>
                <td><?php echo $data['e_name'] ?></td>
                <td><?php echo $data['sort'] ?></td>
                <td>
                    <a href="#">[访问]</a>&nbsp;&nbsp;
                    <a href="cate_edit.php?id=<?php echo $data['id']?>">[修改]</a>&nbsp;&nbsp;
                    <a href="<?php echo $url ?>">[删除]</a>
                </td>
            </tr>
        <?php
        }
        ?>
    </table>
</div>
<?php
include 'inc/footer.inc.php';
?>

2)跳转确认页 confirm.php

confirm 单词本意:确认 [kənˈfɜːm]
<?
include '../inc/config.inc.php';
//做一些基础判断 如果直接访问这个页面 没有参数会报错
//isset();检测变量是否设置,并且不是 NULL。
if (!isset($_GET['url']) || !isset($_GET['back_url']) || !isset($_GET['msg'])) {
exit('请不要直接访问该页面');
}
?>
<!--跳转中转页面-->
<!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>
你确定要删除:<b><? echo $_GET['msg']?>&nbsp;&nbsp;&nbsp;&nbsp;</b>
<a style="color: red"href="<?php echo $_GET['url'] ?>">确定</a>&nbsp;&nbsp; |&nbsp;&nbsp;
<a style="color: green" href="<?php echo $_GET['back_url']?>">取消</a>
</div>
</body>
</html>

3)删除页 cate_del.php

<?php
include_once '../inc/mysql.inc.php';
include_once '../inc/config.inc.php';
include_once '../inc/tool.inc.php';
//get post 传递的数值都是字符串而非整数
$link = connect();

//1 接收验证id值
$id = $_GET['id'];
if (!isset($id) || !is_numeric($id)) {
skip('cate_index.php','error','参数不合法');
}
//2 先查询下该分类下是否有子分类
$sql = "select pid from bbs_category where pid = {$id}";
$res = execute($link,$sql);
if (mysqli_num_rows($res)>0) {
skip('cate_index.php','error','该分类下有子类,请先将对应的子类删掉');
}

//3 查询帖子表里面是否有该分类
$sql = "select cate_id from bbs_content where cate_id={$_GET['id']}";
$res = execute($link,$sql);
if (mysqli_num_rows($res)>0) {
skip('cate_index.php','error','该分类下帖子,请先将对应的帖子删除');
}

//3 删除操作
$sql = "update bbs_category set status=-1 where id={$id}";//假删除SQL
//$sql = "delete from bbs_category where id = $id";//真删除SQL
execute($link,$sql);
if (mysqli_affected_rows($link)==1){
skip('cate_index.php','ok','删除成功');
} else {
skip('cate_index.php','error','删除失败');
}

重要函数: mysqli_affected_rows($link);获取上一次mysql操作中受影响的行数

 添加页

大幅度

修改页

大幅度

 

posted @ 2020-12-17 07:45  棉花糖88  阅读(58)  评论(0)    收藏  举报