From 21059fc846856e6fedd4c409e73ffb43b344ec1a Mon Sep 17 00:00:00 2001 From: Yuan Chiu Date: Thu, 9 Apr 2020 01:38:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E3=80=81=E7=B7=A8=E8=BC=AF?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- htdocs/edit.php | 65 ++++++++++++++++++++++++ htdocs/index.php | 2 +- htdocs/lib/Database/DbMessage.php | 83 ++++++++++++++----------------- htdocs/save.php | 32 ++++++++++++ htdocs/view.php | 51 +++++++++++++++++++ 5 files changed, 185 insertions(+), 48 deletions(-) create mode 100644 htdocs/edit.php create mode 100644 htdocs/save.php create mode 100644 htdocs/view.php diff --git a/htdocs/edit.php b/htdocs/edit.php new file mode 100644 index 0000000..59c42e7 --- /dev/null +++ b/htdocs/edit.php @@ -0,0 +1,65 @@ +getDataByid($id)) { + header('Location: index.php'); + } + $title = $item['title']; + $content = $item['content']; +} +else { + $title = ''; + $content = ''; +} + +?> + + + + + + + Document + + +
+

留言板 編輯

+
+
+
+
+ '; + } + ?> +
    +
  • + +
  • +
  • + +
  • +
+
    +
  • +
  • +
+
+
+
+ + \ No newline at end of file diff --git a/htdocs/index.php b/htdocs/index.php index 856ae1b..f87bdd0 100644 --- a/htdocs/index.php +++ b/htdocs/index.php @@ -63,7 +63,7 @@ $list = $db->getList(); '.$item['updated_at'].' diff --git a/htdocs/lib/Database/DbMessage.php b/htdocs/lib/Database/DbMessage.php index a1b44cf..dca4293 100644 --- a/htdocs/lib/Database/DbMessage.php +++ b/htdocs/lib/Database/DbMessage.php @@ -28,8 +28,8 @@ class DbMessage extends Database /** * 取得單筆資料 * - * @param [type] $id - * @return void + * @param int $id + * @return array */ public function getDataByid($id) { @@ -39,59 +39,48 @@ class DbMessage extends Database $query->bindParam(":id", $id); $query->execute(); - $queryResultAll = $query->fetchAll(); + $queryResultAll = $query->fetch(); return $queryResultAll; - // // 如果有查到一筆以上 - // if( count($queryResultAll) >= 1 ) { - // // 製作回傳結果陣列 - // $result = array(); - // foreach($queryResultAll as $key => $thisResult) { - - // if($thisResult['UEnabled'] != '0') { - // $output_enable = true; - // } - // else { $output_enable = false; } - - // if($thisResult['UEnable_NoAppoint'] != '0') { - // $output_enable_noAppoint = true; - // } - // else { $output_enable_noAppoint = false; } - - // array_push($result, - // array( 'user_id' => $thisResult['UID'], - // 'password' => $thisResult['UPassword'], - // 'group_id' => $thisResult['GID'], - // 'group_name' => $thisResult['GName'], - // 'class_id' => $thisResult['CID'], - // 'class_name' => $thisResult['CName'], - // 'enable' => $output_enable, - // 'build_time' => $thisResult['UBuildTime'], - // 'modify_time' => $thisResult['UModifyTime'], - // 'learnStyle_mode' => $thisResult['LMode'], - // 'material_mode' => $thisResult['MMode'], - // 'enable_noAppoint' => $output_enable_noAppoint, - // 'nickname' => $thisResult['UNickname'], - // 'realname' => $thisResult['URealName'], - // 'email' => $thisResult['UEmail'], - // 'memo' => $thisResult['UMemo']) - // ); - // } - // return $result; - // } - // else { - // return null; - // } } - public function insert(Type $var = null) + + /** + * 建立資料 + * + * @param string $title 標題 + * @param string $content 內容 + * @return int 建立後的ID + */ + public function insert($title, $content) { - # code... + $sqlString = "INSERT INTO `".$this->table('message')."` (title,content) VALUES ( :title, :content )"; + + $query = $this->connDB->prepare($sqlString); + $query->bindParam(":title", $title); + $query->bindParam(":content", $content); + $query->execute(); + return $this->connDB->lastInsertId(); } - public function edit() + /** + * 編輯 + * + * @param int $id 編號 + * @param string $title 標題 + * @param string $content 內容 + * @return int 是否有成功 + */ + public function edit($id, $title, $content) { - # code... + $sqlString = "UPDATE `".$this->table('message')."` SET content= :content ,title= :title WHERE id= :id"; + + $query = $this->connDB->prepare($sqlString); + $query->bindParam(":id", $id); + $query->bindParam(":title", $title); + $query->bindParam(":content", $content); + $query->execute(); + return $query->rowCount(); } public function delete($id) diff --git a/htdocs/save.php b/htdocs/save.php new file mode 100644 index 0000000..20ed23d --- /dev/null +++ b/htdocs/save.php @@ -0,0 +1,32 @@ +edit($id, $title, $content)) { + header('Location: view.php?id='.$id); + } + // 修改失敗 + else { + header('Location: edit.php?id='.$id); + } +} +// 新增資料 +else { + // 修改成功 + if ($id = $db->insert($title, $content)) { + header('Location: view.php?id='.$id); + } + // 修改失敗 + else { + header('Location: edit.php'); + } +} diff --git a/htdocs/view.php b/htdocs/view.php new file mode 100644 index 0000000..4a9f535 --- /dev/null +++ b/htdocs/view.php @@ -0,0 +1,51 @@ +getDataByid($id)) { + header('Location: index.php'); +} +$title = $item['title']; +$content = $item['content']; +$updated_at = $item['updated_at']; +$created_at = $item['created_at']; + +?> + + + + + + + Document + + +
+

留言板 -

+
+
+
+ +
+
+
    +
  • +
  • +
+
    +
  • 最後修改:
  • +
  • 建立時間:
  • +
+
+
+ + \ No newline at end of file