文章分類添加功能
1,編寫文章分類添加模板
新建categoryListHtml.php文件,代碼如下:
<?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>
界面展示如下:
2,輸入分類信息進(jìn)行表單提交
新建category.php文件獲取表單提交的數(shù)據(jù),然后將數(shù)據(jù)寫入到數(shù)據(jù)庫中,具體代碼如下:
<?php require('./init.php'); //獲取操作標(biāo)識 $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';
點(diǎn)擊添加:
數(shù)據(jù)庫展示如下:
點(diǎn)擊添加后觀察數(shù)據(jù)庫信息: