0%

[技能檢定]題組四 步驟11 製作後台管理者新增與修改

  1. ./back/ 目錄中建立 add_admin.php 檔案,並建立新增管理帳號的頁面及表單
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    <h2 class="ct">新增管理帳號</h2>
    <!--使用form表單來新增資料-->
    <!-- form:post>table.all>tr*3>td.tt.ct+td.pp>input:text -->
    <form action="./api/save_admin.php" method="post">
    <table class="all">
    <tr>
    <td class="tt ct">帳號</td>
    <td class="pp"><input type="text" name="acc" id="acc"></td>
    </tr>
    <tr>
    <td class="tt ct">密碼</td>
    <td class="pp"><input type="password" name="pw" id="pw"></td>
    </tr>
    <tr>
    <td class="tt ct">權限</td>
    <td class="pp">
    <!--使用陣列來儲存權限資料-->
    <input type="checkbox" name="pr[]" value="1">商品分類與管理<br>
    <input type="checkbox" name="pr[]" value="2">訂單管理<br>
    <input type="checkbox" name="pr[]" value="3">會員管理<br>
    <input type="checkbox" name="pr[]" value="4">頁尾版權區管理<br>
    <input type="checkbox" name="pr[]" value="5">最新消息管理<br>
    </td>
    </tr>
    </table>
    <div class="ct">
    <input type="submit" value="新增">
    <input type="reset" value="重置">
    </div>
    </form>

  1. ./api/ 目錄中建立 save_admin.php 檔案,並撰寫新增/更新管理帳號的程式碼
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    include_once "db.php";

    //使用序列化來儲存權限資料
    $_POST['pr']=serialize($_POST['pr']);

    //將表單資料儲存至資料表中
    $Admin->save($_POST);

    //導回後台管理頁面
    to("../back.php?do=admin");
  2. 複製一份 ./back/add_admin.php 並改名為 edit_admin.php ,然後根據 $_GET['id'] 的值來取出資料
  3. 要記得在 edit_admin.php 中增加一個隱藏欄位把資料的 id 值帶入
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    //取出單筆管理員資料
    $rows=$Admin->find($_GET['id']);

    //使用unserialize來將序列化的權限資料轉回陣列
    $row['pr']=unserialize($row['pr']);
    ?>
    <h2 class="ct">修改管理員權限</h2>
    <!--使用form表單來修改資料-->
    <form action="./api/save_admin.php" method="post">
    <table class="all">
    <tr>
    <td class="tt ct">帳號</td>
    <td class="pp"><input type="text" name="acc" value="<?=$row['acc'];?>"></td>
    </tr>
    <tr>
    <td class="tt ct">密碼</td>
    <td class="pp"><input type="password" name="pw" value="<?=$row['pw'];?>"></td>
    </tr>
    <tr>
    <td class="tt ct">權限</td>
    <td class="pp">
    <!--檢查資料權限,並勾選已獲得的權限-->
    <input type="checkbox" name="pr[]" value="1" <?=(in_array(1,$row['pr']))?'checked':'';?>>商品分類與管理<br>
    <input type="checkbox" name="pr[]" value="2" <?=(in_array(2,$row['pr']))?'checked':'';?>>訂單管理<br>
    <input type="checkbox" name="pr[]" value="3" <?=(in_array(3,$row['pr']))?'checked':'';?>>會員管理<br>
    <input type="checkbox" name="pr[]" value="4" <?=(in_array(4,$row['pr']))?'checked':'';?>>頁尾版權區管理<br>
    <input type="checkbox" name="pr[]" value="5" <?=(in_array(5,$row['pr']))?'checked':'';?>>最新消息管理<br>
    </td>
    </tr>
    </table>
    <div class="ct">
    <!--使用隱藏欄位來傳送資料id-->
    <input type="hidden" name="id" value="<?=$row['id'];?>">
    <input type="submit" value="修改">
    <input type="reset" value="重置">
    </div>
    </form>