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; } ```
//新增活動 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; } ```
進階搜尋