轻言放弃之博客003
2017-08-18 本文已影响8人
土乒76
栏目列表
catlist.php
// 注意:cat下的大多数操作是根据cat_id来完成的
$conn = mysql_connect('localhost', 'root', '');
mysql_query('use blog', $conn);
mysql_query('set names utf8');
$sql = "select * from cat";
$rs = mysql_query($sql);
$cat = array();
while( $row = mysql_fetch_assoc($rs) ) {
$cat[] = $row;
}
// print_r($cat);// 二维数组
require('./view/admin/catlist.html');
catlist.html
<?php foreach($cat as $key => $value) { ?>
<tr>
<td><?php echo $value["cat_id"] ?></td>
<td><?php echo $value["catname"] ?></td>
<td><span class="badge"><?php echo $value["num"] ?></span></td>
<td>
<a href="catdel.php?cat_id=<?php echo $value["cat_id"]; ?>">删除</a>|
<a href="catedit.php?cat_id=<?php echo $value["cat_id"]; ?>">编辑</a>
</td>
</tr>
<?php } ?>
catdel.php
// 通过cat_id去删除
<?php
$cat_id = $_GET['cat_id'];
$conn = mysql_connect('localhost', 'root', '');
mysql_query('use blog', $conn);
mysql_query('set names utf8');
// 检测栏目ID是否为数字
if( !is_numeric($cat_id) ) {// 数字字符串也行喔
echo "栏目不合法";
exit();
}
// 检测栏目是否存在
$sql = "select count(*) from cat where cat_id = $cat_id";
$rs = mysql_query($sql);
if( mysql_fetch_row($rs)[0] == 0 ) {
echo "栏目不存在";
exit();
}
// 检测栏目下是否有文章
$sql = "select count(*) from art where cat_id=$cat_id";
$rs = mysql_query($sql);
if( mysql_fetch_row($rs)[0] != 0 ) {// 当前栏目下有文章
echo "栏目下有文章不能删除";
exit();
}
// 检测完毕,删除
$sql = "delete from cat where cat_id=$cat_id";
if( mysql_query($sql) ) {// 删除返回的是true or false
echo "栏目删除成功";
}
else {
echo "栏目删除失败";
}
?>
catedit.php
<meta charset="utf8">
<?php
$cat_id = $_GET['cat_id'];
$conn = mysql_connect('localhost', 'root', '');
mysql_query('use blog', $conn);
mysql_query('set names utf8');
// 先检查
// 检测栏目ID是否为数字
if( !is_numeric($cat_id) ) {// 数字字符串也行喔
echo "栏目不合法";
exit();
}
// 检测栏目是否存在
$sql = "select count(*) from cat where cat_id = $cat_id";
$rs = mysql_query($sql);
if( mysql_fetch_row($rs)[0] == 0 ) {
echo "栏目不存在";
exit();
}
if( empty($_POST) ) {// 没有点提交呢
$sql = "select catname from cat where cat_id=$cat_id";// 根据cat_id找cat_name
$rs = mysql_query($sql);
$cat = mysql_fetch_assoc($rs);// 视图中输出$cat['catname']
require('./view/admin/catedit.html');
}
else {// 有post点提交了
$sql = "update cat set catname='$_POST[catname]' where cat_id=$cat_id";// 根据cat_id修改cat_name
if( !mysql_query($sql) ) {
echo "栏目修改失败";
}
else {
echo "栏目修改成功";
}
}
?>