新增、編輯功能
This commit is contained in:
parent
3441965fc0
commit
21059fc846
65
htdocs/edit.php
Normal file
65
htdocs/edit.php
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once 'config.php';
|
||||||
|
require_once FOLDER_ROOT.'/lib/Database/DbMessage.php';
|
||||||
|
use MessageBoard\Database\DbMessage;
|
||||||
|
|
||||||
|
if (!empty($_GET["id"])) {
|
||||||
|
$id = $_GET["id"];
|
||||||
|
$db = new DbMessage();
|
||||||
|
if(!$item = $db->getDataByid($id)) {
|
||||||
|
header('Location: index.php');
|
||||||
|
}
|
||||||
|
$title = $item['title'];
|
||||||
|
$content = $item['content'];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$title = '';
|
||||||
|
$content = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Document</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1>留言板 編輯</h1>
|
||||||
|
</header>
|
||||||
|
<div id="main">
|
||||||
|
<section>
|
||||||
|
<form action="save.php" method="POST">
|
||||||
|
<?php
|
||||||
|
if (!empty($id)) {
|
||||||
|
echo '<input type="hidden" name="id" value="'.$id.'">';
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
<label>
|
||||||
|
<span>Title: </span>
|
||||||
|
<input type="text" name="title" value="<?php echo $title; ?>" required />
|
||||||
|
<strong><abbr title="required">*</abbr></strong>
|
||||||
|
</label>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<label>
|
||||||
|
<span>Content: </span>
|
||||||
|
<textarea name="content"><?php echo $content; ?></textarea>
|
||||||
|
</label>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<ul>
|
||||||
|
<li><input type="submit" class="btn" value="儲存"></li>
|
||||||
|
<li><input type="reset" class="btn" value="回復"></li>
|
||||||
|
</ul>
|
||||||
|
</form>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -63,7 +63,7 @@ $list = $db->getList();
|
|||||||
<td>'.$item['updated_at'].'</td>
|
<td>'.$item['updated_at'].'</td>
|
||||||
<td>
|
<td>
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="" class="btn">Edit</a></li>
|
<li><a href="edit.php?id='.$item['id'].'" class="btn">Edit</a></li>
|
||||||
<li><a href="" class="btn">Delete</a></li>
|
<li><a href="" class="btn">Delete</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</td>
|
</td>
|
||||||
|
@ -28,8 +28,8 @@ class DbMessage extends Database
|
|||||||
/**
|
/**
|
||||||
* 取得單筆資料
|
* 取得單筆資料
|
||||||
*
|
*
|
||||||
* @param [type] $id
|
* @param int $id
|
||||||
* @return void
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getDataByid($id)
|
public function getDataByid($id)
|
||||||
{
|
{
|
||||||
@ -39,59 +39,48 @@ class DbMessage extends Database
|
|||||||
$query->bindParam(":id", $id);
|
$query->bindParam(":id", $id);
|
||||||
$query->execute();
|
$query->execute();
|
||||||
|
|
||||||
$queryResultAll = $query->fetchAll();
|
$queryResultAll = $query->fetch();
|
||||||
|
|
||||||
return $queryResultAll;
|
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)
|
public function delete($id)
|
||||||
|
32
htdocs/save.php
Normal file
32
htdocs/save.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
$title = $_POST['title'];
|
||||||
|
$content = $_POST['content'];
|
||||||
|
|
||||||
|
require_once 'config.php';
|
||||||
|
require_once FOLDER_ROOT.'/lib/Database/DbMessage.php';
|
||||||
|
use MessageBoard\Database\DbMessage;
|
||||||
|
$db = new DbMessage();
|
||||||
|
|
||||||
|
// 修改資料
|
||||||
|
if (!empty($_POST['id'])) {
|
||||||
|
$id = $_POST['id'];
|
||||||
|
// 修改成功
|
||||||
|
if ($db->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');
|
||||||
|
}
|
||||||
|
}
|
51
htdocs/view.php
Normal file
51
htdocs/view.php
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
$id = $_GET["id"];
|
||||||
|
|
||||||
|
require_once 'config.php';
|
||||||
|
require_once FOLDER_ROOT.'/lib/Database/DbMessage.php';
|
||||||
|
use MessageBoard\Database\DbMessage;
|
||||||
|
|
||||||
|
|
||||||
|
$db = new DbMessage();
|
||||||
|
if(!$item = $db->getDataByid($id)) {
|
||||||
|
header('Location: index.php');
|
||||||
|
}
|
||||||
|
$title = $item['title'];
|
||||||
|
$content = $item['content'];
|
||||||
|
$updated_at = $item['updated_at'];
|
||||||
|
$created_at = $item['created_at'];
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Document</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1>留言板 - <?php echo $id; ?></h1>
|
||||||
|
</header>
|
||||||
|
<div id="main">
|
||||||
|
<div class="func-btn">
|
||||||
|
<ul>
|
||||||
|
<li><a href="index.php" class="btn">Index</a></li>
|
||||||
|
<li><a href="edit.php?id=<?php echo $id; ?>" class="btn">Edit</a></li>
|
||||||
|
<li><a href="edit.php" class="btn">Delete</a></li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<section>
|
||||||
|
<ul>
|
||||||
|
<li><?php echo $title; ?></li>
|
||||||
|
<li><?php echo $content; ?></li>
|
||||||
|
</ul>
|
||||||
|
<ul>
|
||||||
|
<li>最後修改:<?php echo $updated_at; ?></li>
|
||||||
|
<li>建立時間:<?php echo $created_at; ?></li>
|
||||||
|
</ul>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user