:::

10-3 寫入活動到資料庫

  1. 由於有隱藏 op=insert_action,所以,在 admin.php 中的 switch 中多一組對應設定:
    case "insert_action":
        $action_id = insert_action();
        header("location:index.php?action_id=$action_id");
        exit;
  2. 設定要去執行 insert_action() 函數,也就是用來新增活動到資料庫,並希望他能傳回活動編號,以便儲存後,可以直接連到首頁index.php去觀看該活動的詳細內容。

  3. 接著製作 insert_action() 函數,由於寫入的過程都差不多,在此會建議直接複製之前的寫入函數來修改會更快:

    //新增活動
    function insert_action()
    {
    
        global $db;
        $title       = clean_var('title', '活動名稱');
        $action_date = clean_var('action_date', '活動日期');
        $end_date    = clean_var('end_date', '截止日期');
        $enable      = clean_var('enable', '使否啟用');
        $content     = clean_var('content', '活動內容');
        $uid         = $_SESSION['uid'];
    
        $sql = "INSERT INTO `actions` ( `title`, `content`, `action_date`, `end_date`, `uid`, `enable`)
        VALUES ('{$title}', '{$content}', '{$action_date}', '{$end_date}', '{$uid}', '{$enable}')";
        if (!$db->query($sql)) {
            throw new Exception($db->error);
        }
        $action_id = $db->insert_id;
        return $action_id;
    }

     


:::

書籍目錄

展開 | 闔起

http%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbsn%3D27%26tbdsn%3D732

計數器

今天: 203203203
昨天: 1953195319531953
總計: 7481104748110474811047481104748110474811047481104