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

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

設計後台新增新訊的表單

admin_news.php 檔案中設計新增新訊內容的表單。

<form enctype="multipart/form-data" method="post" action="" class="w3-text-grey w3-row form-news">
 
  <div class="w3-third w3-center">
    <!-- 以label標籤包含input標籤, 讓input標籤隱藏, 設計label標籤呈現視覺看到的樣子 -->
    <!-- 當按到label標籤時, 即代表了按到了input標籤(選擇檔案準備上傳檔案) -->
    
<label class="w3-button w3-grey w3-round w3-padding cursor-p"> <input type="file" name="news_img" id="news_img" style="display:none;"> <i class="fa fa-photo"></i> 上傳圖片 </label> <!--#imgArea 這個區域負責顯示所選擇的圖檔, 也就是影像的預覽區域 --> <div id="imgArea" title="" style="padding:20px;"> <img class="imgArea" style="max-width:100%;height:auto;"> </div>
</div>   <div class="w3-twothird">   <div class="w3-row"> <div class="w3-col"><i class="w3-xxlarge fa fa-folder-open"></i></div> <div class="w3-rest select_news_class"> <!-- 這裡是下拉式選單選擇新訊分類 --> <select name="news_class_id" class="w3-select w3-border" required> <option value="">選擇新訊的分類...</option>   <option value=""></option>   </select> </div> </div>   <div class="w3-row"> <div class="w3-col"><i class="w3-xxlarge fa fa-flag"></i></div> <div class="w3-rest"> <!-- 這裡是新訊標題欄位 --> <input type="text" name="news_title" maxlength="30" class="w3-input w3-border" placeholder="輸入新訊的標題" required> </div> </div>   <div class="w3-row"> <div class="w3-col"><i class="w3-xxlarge fa fa-commenting"></i></div> <div class="w3-rest"> <!-- 這裡是新訊內容欄位 --> <textarea name="news_content" class="w3-input w3-border" rows="6" placeholder="輸入新訊的內容" required="" required></textarea> </div> </div>   <div class="w3-row"> <div class="w3-col"><i class="w3-xxlarge fa fa-map-marker"></i></div> <div class="w3-rest"> <!-- 這裡是設計是否為焦點新訊的欄位 --> <label class="cursor-p"> <input type="radio" name="news_focus" class="w3-radio" value="1"> 是焦點新訊</label> <label class="cursor-p"> <input type="radio" name="news_focus" class="w3-radio" value="0" checked> 非焦點新訊</label> <div class="w3-right"> <label class="cursor-p"> <input type="checkbox" name="news_show" class="w3-check" value="1" checked> 顯示</label> </div> </div> </div>   </div>   <div class="form-btn-area"> <a href="javascript:window.history.back();" class="form-btn">不新增新訊回上一頁</a> <input type="submit" class="form-btn" value="確定新增新訊"> </div>   <input type="hidden" name="form-name" value="form-news"> </form>

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

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

<?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("Errpr!: ". $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>

新增新訊到資料庫

//判斷是否為新增新訊====================================================
if( isset($_POST['form-name']) && $_POST['form-name']=='form-news' ){
  try {
    $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("Errpr!: ". $e->getMessage());
  }
}

 

 

go TOP