WEB ◆ TS Library ◆ 熱衷分享 ◆ 享受教學相長 ◆ 無形的網絡擁有熱情溫度的傳遞

3-3. 新訊系統的新增工作

講義參考用,要活用變化,或許要修改喔~

先準備「新增新訊」的表單

admin_news.php 檔案中設計新增新訊內容的表單,表單action送到 admin_news_process.php

設計下拉式選單選擇新訊分類

由於下拉式選單要選擇的是新訊的分類,所以得先建立新訊分類的資料集,再設計下拉式選單。

<?php
require_once('../shared/conn_pdo.php');
include_once('../shared/assist.php');
 
//資料集:新訊分類=================================================
try {
  $sql_str = "SELECT * 
              FROM news_class 
              ORDER BY news_class_id ASC";
  $RS_news_class = $conn -> query($sql_str);
} 
catch ( PDOException $e ){
  die("ERROR!!!: ". $e->getMessage());
}
?>
<!-- 這裡是下拉式選單選擇新訊分類 -->
<select name="news_class_id" class="w3-select w3-border" required>
  <option value="">選擇新訊的分類...</option>

  <?php foreach( $RS_news_class as $row_RS_news_class ){ ?>
  <option value="<?php echo $row_RS_news_class['news_class_id']; ?>">
    <?php echo $row_RS_news_class['news_class_name']; ?>
  </option>
  <?php } ?>

</select>

新增新訊到資料庫

admin_news_process.php 檔案。

//判斷是否為新增新訊====================================================
if( isset($_POST['MM_process']) && $_POST['MM_process']=='insert' ){
  try {
    //準備SQL語法>建立預處理器-------------------------------------------
    $sql_str = "INSERT INTO news ( news_class_id, news_title, news_content, 
                                   news_focus, news_show, news_time_c, news_time_m ) 
                          VALUES ( :news_class_id, :news_title, :news_content, 
                                   :news_focus, :news_show, :news_time_c, :news_time_m )";
    $stmt = $conn -> prepare($sql_str);
 
    //接收資料---------------------------------------------------------
    $news_class_id = $_POST['news_class_id'];
    $news_title    = $_POST['news_title'];
    $news_content  = $_POST['news_content'];
    $news_focus    = $_POST['news_focus'];
    isset($_POST['news_show'])? $news_show = 1 : $news_show = 0; 
    $news_time_c   = date('Y-m-d H:i:s');
    $news_time_m   = date('Y-m-d H:i:s');
 
    //綁定資料---------------------------------------------------------
    $stmt -> bindParam(':news_class_id', $news_class_id);
    $stmt -> bindParam(':news_title'   , $news_title);
    $stmt -> bindParam(':news_content' , $news_content);
    $stmt -> bindParam(':news_focus'   , $news_focus);
    $stmt -> bindParam(':news_show'    , $news_show);
    $stmt -> bindParam(':news_time_c'  , $news_time_c);
    $stmt -> bindParam(':news_time_m'  , $news_time_m);
 
    //執行-------------------------------------------------------------
    $stmt -> execute();
    //$news_id = $conn -> lastInsertId();  //echo $news_id;
 
    header('Location:./?page=admin_news');
  } 
  catch ( PDOException $e ){
    die("ERROR!!!: ". $e->getMessage());
  }
}

 

 

go TOP