Lu-yeom / mentor-program-5th-self-learning

0 stars 0 forks source link

week11(2021/6/21~2021/6/27) #10

Open Lu-yeom opened 3 years ago

Lu-yeom commented 3 years ago

日期:110年6月21日~23日
今日進度:[BE101] 用 PHP 與 MySQL 學習後端基礎:真正的實戰:留言板 - 新增功能篇#1-#5


課程筆記:

一、真正的實戰:留言板 - 新增功能篇

二、編輯暱稱功能

  <form class="hide board__nickname-form board__new-comment-form" method="POST" action="update_user.php">
    <div class="board__nickname">
      <span>新的暱稱:</span>
        <input type="text" name="nickname" />
    </div>
      <input class="board__submit-btn" type="submit" />
  </form>

?>

* 做出隱藏效果  

三、資料庫正規化  
  透過一定的程序,建立資料庫內容之間的關聯,去除資料庫中冗餘的內容,讓資料能夠井然有序且有效率的儲存。  
  * 將資料庫中nickname修改為username  
  * 修正handle_add_comment.php  

<?php session_start(); require_once('conn.php'); require_once('utils.php');

if ( empty($_POST['content']) ) { header('Location: index.php?errCode=1'); die('資料不齊全'); }

$username = $_SESSION['username']; $content = $_POST['content'];

$sql = "insert into comments(username, content) values(?, ?)"; $stmt = $conn->prepare($sql); $stmt->bind_param('ss', $username, $content); $result = $stmt->execute(); if (!$result) { die($conn->error); } header("Location: index.php");

?>


四、SQL join 語法介紹    
  * INNER JOIN:結合兩個表單內都有的資料,如果資料只存在其中一個表單,會被刪除。  
  * LEFT JOIN:將表單1的資料左外部結合進表單2的資料,若表單1的資料在表單2不存在則保留;表單2的資料不存在表單1則刪除。  
  * RIGHT JOIN:與LEFT JOIN相反。  
  * OUTER JOIN:將兩個表單的資料全部結合,若資料只存在其中一個表單,會保留資料,但會出現空值。  
* SQL語法  

SELECT * FROM comments LEFT JOIN users ON comments.username = users.username


五、修正問題:實作留言與會員的 join  

$stmt = $conn->prepare('select C.id as id, C.content as content, '. 'C.create_at as create_at, U.nickname as nickname ', 'from comments as C '. 'left join users as U on C.username = U.username order by C.id desc'); $result = $stmt->execute();

Lu-yeom commented 3 years ago

日期:110年6月24日20:30~22:30
今日進度:[BE101] 用 PHP 與 MySQL 學習後端基礎:真正的實戰:留言板 - 新增功能篇#6-#7


課程筆記:

一、編輯留言功能

<span class="card__time">
  <?php echo escape($row['created_at']); ?>
</span>
<?php if ($row['username'] === $username) { ?>
  <a href="update_comment.php?id=<?php echo $row['id'] ?>">編輯</a>
<? } ?>
</div>
  <p class="card__content"><?php echo escape($row['content']); ?></p>
</div>

在顯示時間後加上編輯功能,且只能編輯自己的留言

<!DOCTYPE html>

留言板
注意!本站為練習用網站,因教學用途刻意忽略資安的實作,註冊時請勿使用任何真實的帳號或密碼。

編輯留言

錯誤:' . $msg . ''; } ?>
*  利用update_user.php建立handle_update_comment.php  

<?php session_start(); require_once('conn.php'); require_once('utils.php');

if ( empty($_POST['content']) ) { header('Location: update_comment.php?errCode=1&id='.$_POST['id']); die('資料不齊全'); }

$username = $_SESSION['username']; $id = $_POST['id']; $content = $_POST['content'];

$sql = "update comments set content=? where id=?"; $stmt = $conn->prepare($sql); $stmt->bind_param('si', $content, $id); $result = $stmt->execute(); if (!$result) { die($conn->error); }

header("Location: index.php"); ?>


二、刪除留言功能  

<?php if ($row['username'] === $username) { ?> 編輯 刪除 <?php } ?>

* 利用handle_update_comment.php製作delete_comment.php  

<?php session_start(); require_once('conn.php'); require_once('utils.php');

if ( empty($_GET['id']) ) { header('Location: index.php?errCode=1'); die('資料不齊全'); }

$id = $_GET['id'];

$sql = "update comments set is_deleted=1 where id=?"; $stmt = $conn->prepare($sql); $stmt->bind_param('i', $id); $result = $stmt->execute(); if (!$result) { die($conn->error); }

header("Location: index.php"); ?>

* delete分為hard delete和soft delete,差別在於hard delete會將資料庫的留言刪除,soft delete則是會在資料庫中新增一筆is_deleted資料,以防誤刪後想復原資料。  
  * comments => 結構 => 於content之後 => 新增is_deleted => 類型TINYINT、預設值NULL => 儲存  
  * ```$sql = "update comments set is_deleted=1 where id=?";```  設定刪除留言的狀態為1  
  * 但是按刪除鍵時留言不會同步消失,所以再修正如下:  

$stmt = $conn->prepare( 'select '. 'C.id as id, C.content as content, '. 'C.created_at as created_at, U.nickname as nickname, U.username as username '. 'from comments as C ' . 'left join users as U on C.username = U.username '. 'where C.is_deleted IS NULL '. 'order by C.id desc' ); $result = $stmt->execute();

Lu-yeom commented 3 years ago

日期:110年6月26日20:30~22:30
今日進度:[BE101] 用 PHP 與 MySQL 學習後端基礎:真正的實戰:留言板 - 新增功能篇#8 - 真正的實戰:留言板 - 再次修正問題篇


課程筆記:

一、實作分頁功能,介紹 offset 與 limit
設定一頁可顯示幾個留言,並分頁(以5個留言一頁為例)

$page = 1;
  if (!empty($_GET['page'])) {
    $page = intval($_GET['page']);
  }
  $items_per_page = 5;
  $offset = ($page - 1) * $items_per_page;

  $stmt = $conn->prepare(
    'select '.
      'C.id as id, C.content as content, '.
      'C.created_at as created_at, U.nickname as nickname, U.username as username '.
    'from comments as C ' .
    'left join users as U on C.username = U.username '.
    'where C.is_deleted IS NULL '.
    'order by C.id desc '.
    'limit ? offset ? '
  );
  $stmt->bind_param('ii', $items_per_page, $offset);
  $result = $stmt->execute();

製作切換頁面的功能

</section>
  <div class="board__hr"></div>
    <?php 
      $stmt = $conn->prepare(
        'select count(id) as count from comments where is_deleted IS NULL'
      );
      $result = $stmt->execute();
      $result = $stmt->get_result();
      $row = $result->fetch_assoc();
      $count = $row['count'];
      $total_page = ceil($count / $items_per_page);
    ?>
      <div class="page-info">
        <span>總共有 <?php echo $count ?> 筆留言,頁數:</span>
        <span><?php echo $page ?> / <?php echo $total_page ?></span>
      </div>
      <div class="paginator">
        <a href="index.php?page=1">首頁</a>
        <?php if ($page != 1) {?>
          <a href="index.php?page=<?php echo $page - 1?>">上一頁</a>
        <?php } ?>
        <?php if ($page != $total_page) {?>
          <a href="index.php?page=<?php echo $page + 1?>">下一頁</a>
          <a href="index.php?page=<?php echo $total_page?>">最後一頁</a>
        <?php } ?>

        </div>

二、發現問題:權限管理問題
雖然頁面設定為只能編輯自己的留言,但其實只要透過網址列更改編號,就可以修改其他用戶的留言,所以必須確定使用者的權限(*超級重要)

三、修正問題:確認權限