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 :
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{ //判斷數(shù)據(jù)庫中是否有同名的分類名稱 $sql="select id from cms_category where name=:name"; if ( $db->data($data)->fetchRow($sql)){ $error[]="該文章分類已存在"; }else{ //插入到數(shù)據(jù)庫 $sql="insert into cms_category(name)values(:name)"; $db->data($data)->query($sql); } } } } require './categoryListHtml.php';
Click to add:
##The database is displayed as follows: