欄目的修改與刪除
前面的章節(jié)我們做了欄目的添加功能和從、欄目的展示列表,在展示頁(yè)面我們有下面這樣一段代碼:
<td> <a class="link-update" href="cate_edit.php?id=<?php echo $v['id'];?>">修改</a> <a class="link-del" onclick="return confirm('確定刪除當(dāng)前數(shù)據(jù)?')" href="cate_del.php?id=<?php echo $v['id']; ?>">刪除</a> </td>
在這裡我們已經(jīng)將對(duì)應(yīng)的欄目id獲取到了,這要在對(duì)應(yīng)的頁(yè)面接收這個(gè)id,然後透過對(duì)資料庫(kù)的操作,就能完成對(duì)欄目的修改與刪除功能。
首先我們先來做修改的功能,點(diǎn)選修改頁(yè)面會(huì)進(jìn)入下面這樣的頁(yè)面:
在這裡我們是將已經(jīng)加入資料庫(kù)的文件給取出來,然後在將資料展示在頁(yè)面上,程式碼如下:
<?php include_once('../../common/config.php'); $id=$_GET['id']; $sql="select * from cate WHERE id='$id'"; $que=mysqli_query($conn,$sql); $row=mysqli_fetch_assoc($que); function getList($pid=0,&$result=array(),$spac=0) { global $conn; $spac +=4; $sql = "select * from cate where pid = $pid"; $res = mysqli_query($conn,$sql); while($rows=mysqli_fetch_array($res)) { $rows["name"] = str_repeat(' ',$spac).'|--'.$rows["name"]; $result[] = $rows; getList($rows['id'],$result,$spac); } return $result; } $rs=getList(); //print_r($rs); //die; ?> <!doctype html> <html> <head> <meta charset="UTF-8"> <title>欄目修改--后臺(tái)管理</title> <link rel="stylesheet" type="text/css" href="../../public/style/css/common.css"/> <link rel="stylesheet" type="text/css" href="../../public/style/css/main.css"/> <script type="text/javascript" src="../../public/style/js/libs/modernizr.min.js"></script> <script type="text/javascript" charset="utf-8" src="../../public/ueditor/ueditor.config.js"></script> <script type="text/javascript" charset="utf-8" src="../../public/ueditor/ueditor.all.min.js"></script> <script type="text/javascript" charset="utf-8" src="../../public/ueditor/lang/zh-cn/zh-cn.js"></script> </head> <body> <?php include_once("../../common/top.php"); ?> <div class="container clearfix"> <?php include("../../common/left.php"); ?> <!--/sidebar--> <div class="main-wrap"> <div class="crumb-wrap"> <div class="crumb-list"><i class="icon-font"></i><a href="../index.php">首頁(yè)</a><span class="crumb-step">></span><a class="crumb-name" href="cate_list.php">欄目管理</a><span class="crumb-step">></span><span>新增作品</span></div> </div> <div class="result-wrap"> <div class="result-content"> <form action="cate_upd.php" method="post" id="myform" name="myform" enctype="multipart/form-data"> <input type="hidden" name="id" value="<?php echo $id;?>"> <table class="insert-tab" width="100%"> <tbody> <tr> <th><i class="require-red">*</i>欄目標(biāo)題:</th> <td> <input class="common-text required" id="title" name="name" size="50" value="<?php echo $row['name'];?>" type="text"> </td> </tr> <tr> <th><i class="require-red">*</i>欄目類型:</th> <td> <select name="modelid" class="form-control"> <option <?php if($row['modelid']==1){ echo "selected"; } ?> value='1'>文章模型</option>; <option <?php if($row['modelid']==2){ echo "selected"; } ?> value='2'>單頁(yè)模型</option>; <option <?php if($row['modelid']==3){ echo "selected"; } ?> value='3'>產(chǎn)品模型</option>; <option <?php if($row['modelid']==4){ echo "selected"; } ?> value='4'>圖片模型</option>; ?> </select> </td> </tr> <tr> <th><i class="require-red">*</i>欄目狀態(tài):</th> <td> <input type='radio' <?php if($row['status']==1){ echo "checked"; } ?> name='status' value='1'/>顯示   <input type='radio' <?php if($row['status']==0){ echo "checked"; } ?> name='status' value='0'/>隱藏 </td> <tr> <th>關(guān)鍵詞:</th> <td> <input class="common-text required" id="title" name="keywords" size="50" value="<?php echo $row['keywords'];?>" type="text"> </td> </tr> <tr> <th>內(nèi)容:</th> <td><textarea name="content" class="common-textarea" id="content" cols="30" style="width: 98%;" rows="10"><?php echo $row['content'] ?></textarea></td> </tr> <tr> <th></th> <td> <input class="btn btn-primary btn6 mr10" value="提交" type="submit"> <input class="btn btn6" onclick="history.go(-1)" value="返回" type="button"> </td> </tr> </tbody></table> </form> </div> </div> </div> <!--/main--> </div> </body> </html> <script type="text/javascript"> UE.getEditor('content',{initialFrameWidth:1100,initialFrameHeight:400,}); </script>
資料取了出來,然後在將資料傳送本對(duì)應(yīng)的頁(yè)面,在接收一下,使用sql語句存到資料庫(kù)即可,當(dāng)然也可以在本頁(yè)面接收數(shù)據(jù),只要將form表單的action屬性設(shè)為空就可以了,下面是接收數(shù)據(jù)修改頁(yè)面的程式碼
<?php include_once('../../common/config.php'); $id=$_POST['id']; $name=$_POST['name']; $modelid=$_POST['modelid']; $status=$_POST['status']; $keywords=$_POST['keywords']; $content=$_POST['content']; $sqlfy = "select * from cate WHERE id='$id'"; $fy_que=mysqli_query($conn,$sqlfy); if(empty($fy_que)){ echo "該分類不存在"; exit; } if($_POST){ $sql= "update cate set name='$name',modelid='$modelid',status='$status',keywords='$keywords',content='$content' where id='$id'"; // print_r($sql); // die; $que=mysqli_query($conn,$sql); if($que){ echo "<script>alert('修改成功');location.href='cate_list.php';</script>"; }else{ echo "<script>alert('修改失敗,請(qǐng)檢查后在提交');Location.href='cate_edit.php';</script>"; } }
好了,這樣我們的修改就完成了,下面開始我們的刪除。
刪除這裡要注意的是,因?yàn)槲覀兊臋谖挥卸?jí)分類,所以當(dāng)你要?jiǎng)h除頂級(jí)分類的時(shí)候,一定要先刪除頂級(jí)分類下的子類,不然是不允許刪除的, diamante如下:
<?php include_once('../../common/config.php'); $id=$_GET['id']; $fy_sql="select count(*) AS c from cate WHERE pid={$id}"; $fy_que=mysqli_query($conn,$fy_sql); $fy_row=mysqli_fetch_array($fy_que); if($fy_row['c']>0){ $sql="delete from cate where id='$id' "; $que=mysqli_query($conn,$sql); if($que){ echo"<script>alert('刪除成功,返回首頁(yè)');location.href='cate_list.php';</script>"; }else{ echo "<script>alert('刪除失敗');location='" . $_SERVER['HTTP_REFERER'] . "'</script>"; exit; } }else{ echo "<script>alert('請(qǐng)先刪除子類');location='" . $_SERVER['HTTP_REFERER'] . "'</script>"; }