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

3-5. 新訊系統的修改工作

設計 news_modify.php 編輯修改新訊

新訊的修改工作:相當於新增資料的表單=>查詢出要修改的單筆資料=>在表單中顯示查詢的資料=>修改更新資料庫。

可以將新增新訊中的表單複製過來再稍微修改。

<h1>編輯新訊</h1>
 
<form name="newsModiForm" method="post" action="" 
      class="w3-container w3-text-grey w3-margin w3-row">
 
  <div class="w3-third w3-center">上傳圖片</div>
 
  <div class="w3-twothird">
 
    <!-- 第一個欄位: 新訊分類 -->
    <div class="w3-row w3-section">
      <div class="w3-col" style="width:50px;text-align:center;">
        <i class="w3-xxlarge fa fa-folder-open"></i>
      </div>
      <div class="w3-rest select_news_class">
        <select name="news_class_id" required class="w3-select w3-border">
          <option value="" disabled selected>選擇新訊的分類...</option>
 
          <option value=""> </option>
 
        </select>
      </div>
    </div>
 
    <!-- 第二個欄位: 新訊標題 -->
    <div class="w3-row w3-section">
      <div class="w3-col" style="width:50px;text-align:center;">
        <i class="w3-xxlarge fa fa-flag"></i>
      </div>
      <div class="w3-rest">
        <input type="text" name="news_title" required 
               placeholder="輸入新訊的標題" class="w3-input w3-border">
      </div>
    </div>
 
    <!-- 第三個欄位: 新訊內容 -->
    <div class="w3-row w3-section">
      <div class="w3-col" style="width:50px;text-align:center;">
        <i class="w3-xxlarge fa fa-commenting"></i>
      </div>
      <div class="w3-rest">
        <textarea name="news_content" required class="w3-input w3-border"
                  placeholder="輸入新訊的內容" rows="10"></textarea>
      </div>
    </div>
 
    <!-- 第四個欄位: 是否為焦點新訊 -->
    <div class="w3-row w3-section">
      <div class="w3-col" style="width:50px;text-align:center;">
        <i class="w3-xxlarge fa fa-map-marker"></i>
      </div>
      <div class="w3-rest">
        <label><input type="radio" required name="news_focus" value="1" class="w3-radio"> 
        是焦點新訊</label> &nbsp;&nbsp; 
        <label><input type="radio" required name="news_focus" value="0" class="w3-radio" checked>
        不是焦點新訊</label>
      </div>
    </div>
 
  </div>
 
  <input type="hidden" name="formname" value="newsModiForm">
  <input type="submit" class="form-btn" value="確定修改新訊">
  <input type="button" class="form-btn" value="不修改回上一頁"
         onclick="window.history.back();">
 
</form>

連線資料庫,建立新訊分類資料集=>設計在下拉式選單

<?php
require_once('shared/conn_pdo.php');
 
//建立news_class的資料集=>下拉式選單用=============
try{
  $sql_str = "SELECT * FROM news_class 
              ORDER BY news_class_id ASC";
  $RS_news_class = $conn -> query($sql_str);
  $total_RS_news_class = $RS_news_class -> rowCount();
  //print_r($RS_news_class);
  //echo $total_RS_news_class.'筆';
}
catch ( PDOException $e ){
  die("Errpr!: ". $e->getMessage());
}
?>
<select name="news_class_id" required class="w3-select w3-border">
  <option value="" disabled selected>選擇新訊的分類...</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>

查詢出要修改的單筆資料

$news_id = $_GET['news_id'];
//資料集:新訊內容 ======================================================
try{
  $sql_str = "SELECT news.*, news_class.news_class_name
              FROM news 
              LEFT JOIN news_class
              ON news.news_class_id = news_class.news_class_id
              WHERE news_id = :news_id";
 
  $RS_news = $conn->prepare($sql_str);
  $RS_news->bindParam(':news_id', $news_id);
  $RS_news->execute();
  $row_RS_news = $RS_news->fetch(PDO::FETCH_ASSOC);
}
catch (PDOException $e ){
  die("Errpr!: ". $e->getMessage());
}

查詢出的資料顯示到表單中

<!-- 這裡是下拉式選單選擇新訊分類 -->
<option value="<?php echo $row_RS_news_class['news_class_id']; ?>"
  <?php 
  if($row_RS_news['news_class_id']==$row_RS_news_class['news_class_id']){ 
    echo 'selected'; } 
  ?>>
<!-- 這裡是新訊標題欄位 -->
<input type="text" name="news_title" maxlength="30" class="w3-input w3-border" 
  placeholder="輸入新訊的標題" required value="<?php echo $row_RS_news['news_title']; ?>">
<!-- 這裡是新訊內容欄位 -->
<textarea name="news_content" class="w3-input w3-border" rows="12" 
  placeholder="輸入新訊的內容" 
  required><?php echo $row_RS_news['news_content']; ?></textarea>
<!-- 這裡是設計是否為焦點新訊的欄位 -->
<label class="cursor-p">
  <input type="radio" name="news_focus" class="w3-radio" value="1" 
    <?php if($row_RS_news['news_focus']==1){ echo 'checked'; } ?>> 是焦點新訊</label>
<label class="cursor-p">
  <input type="radio" name="news_focus" class="w3-radio" value="0" 
    <?php if($row_RS_news['news_focus']==0){ echo 'checked'; } ?>> 非焦點新訊</label>
<input type="hidden" name="news_id" value="<?php echo $row_RS_news['news_id']; ?>">

修改更新資料庫

if( isset($_POST['form-name']) && $_POST['form-name']=='form-news-modify' ){
  try {
    $sql_str = "UPDATE news SET news_class_id = :news_class_id, 
                                news_title    = :news_title   , 
                                news_content  = :news_content , 
                                news_focus    = :news_focus   , 
                                news_time_m   = :news_time_m  
                          WHERE news_id       = :news_id";
    $stmt = $conn -> prepare($sql_str);
 
    $news_id       = $_POST['news_id'];
    $news_class_id = $_POST['news_class_id'];
    $news_title    = $_POST['news_title'];
    $news_content  = $_POST['news_content'];
    $news_focus    = $_POST['news_focus'];
    $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_time_m'  , $news_time_m);
    $stmt -> bindParam(':news_id'      , $news_id);
 
    $stmt -> execute();
    header('Location:./?page=news_index&w=a');
}
catch (PDOException $e ){
  die("Error!: ". $e->getMessage());
}
?>

 

 

go TOP