PHP7实战开发简单CMS内容管理系统(12)栏目修改

时间:2022-09-19 15:34:45

1.封装取一条数据函数

 1 //取单条数据
 2 function sql_get_data_one($sql)
 3 {
 4     //取多条数据
 5     $data = sql_get_data($sql);
 6     if ($data)
 7     {
 8         return $data[0];
 9     }else
10     {
11         return false;
12     }
13 }

2.栏目修改具体代码

 1 <?php
 2 require_once('../config.php');
 3 require_once('../functions.php');
 4 check_login();
 5 
 6 function edit_category()
 7 {
 8     if(empty($_POST['catename'])||empty($_POST['slug'])||empty($_GET['id']))
 9     {
10         $GLOBALS['message']="必填数据不能为空";
11         return;
12     }
13     $catename = $_POST['catename'];
14     $slug = $_POST['slug'];
15     $id = $_GET['id'];
16     $affected_rows = sql_excute("UPDATE ft_category SET catename='{$catename}',slug='{$slug}' WHERE id='{$id}';");
17     if ($affected_rows >0)
18     {
19         header('Location:./category_list.php');
20     }else
21     {
22         $GLOBALS['message'] = '栏目名修改失败';
23         return;
24     }
25 }
26 
27 if($_SERVER['REQUEST_METHOD'] == 'POST'){
28     //修改保存数据
29     edit_category();
30 }
31 else
32 {//显示要修改的数据
33     if(!empty($_GET['id']))
34     {
35         $id = $_GET['id'];
36         if(is_numeric($id))
37         {
38             $category_one = sql_get_data_one("select * from ft_category WHERE id ='{$id}'");
39 
40         }
41     }
42 
43 
44 }
45 ?>

3.模板

 <input value="<?php echo $category_one['catename'] ;?>" class="form-control" id="username" placeholder="" name="catename" required="" type="text">

4.修改按钮

<a href="./category_edit.php?id=<?php echo  $c['id'];?>" class="btn btn-primary btn-sm shiny">