Article classification added function

1, write article categories and add templates

Create a new categoryListHtml.php file, the code is as follows:

<?php
header("Content-Type:text/html;charset=utf-8");
?>
<h1>后台文章分类管理</h1>
<!--添加文章分类功能-->
<form action="?action=add" method="post">
    分类名称:<input type="text" name="name">
    <input type="submit" value="添加">
</form>

The interface is shown as follows :

微信图片_20180306151229.png

2, enter the category information to submit the form

Create a new category.php file Get the data submitted by the form, and then write the data into the database. The specific code is as follows:

<?php
require('./init.php');
//获取操作标识
$a=isset($_GET['action'])?$_GET['action']:"";
//文章分类具体功能
if($a=='add'){
    $data['name']=trim(htmlspecialchars($_POST['name']));
    //判断分类名称是否为空
    if($data['name']===''){
        $error[]='文章分类名称不能为空!';
    }else{
        //判断数据库中是否有同名的分类名称
        $sql="select id from cms_category where name=:name";
        if ( $db->data($data)->fetchRow($sql)){
            $error[]="该文章分类已存在";
        }else{
            //插入到数据库
            $sql="insert into cms_category(name)values(:name)";
            $db->data($data)->query($sql);
        }
    }
}
}
require './categoryListHtml.php';

Click to add:

微信图片_20180306152710.png

##The database is displayed as follows:

微信图片_20180306152841.png

微信图片_20180306152710.png

Click to add and observe the database information:


Continuing Learning
||
<?php echo "文章分类添加功能";
submitReset Code