資料來源: http://cms.cc.nthu.edu.tw/moodle/mod/resource/view.php?id=218
http://cms.cc.nthu.edu.tw/moodle/mod/resource/view.php?id=221 >>> 這裡有完整steps
=========================================================
0. 先建立對應之資料庫
=========================================================
1. 入口
<html>
<head>
<title>留言板</title>
</head>
<body>
<a href="view.php">觀看留言</a><p>
<form name="form1" method="post" action="add.php">
姓名:<input type="text" name="name"><br>
郵件:<input type="text" name="mail"><br>
主題:<input type="text" name="subject"><br>
內容:<textarea rows=7 name="content"></textarea><br>
<input type="submit" name="Submit" value="送出">
<input type="Reset" name="Reset" value="重新填寫">
</form>
</body>
</html>
=========================================================
2. db.php
<html>
<head>
<title>討論區</title>
</head>
<body>
<?php
/* 連接資料庫 */
mysql_connect("localhost","root","0410");
mysql_select_db("board");
?>
</body>
</html>
==========================================================
3. add.php
<?header("location:view.php");?> //重新導向到view.php檔案
<?php
include("db.php"); //匯入db.php檔案
/* 接收表單資料 */
$name=$_POST["name"];
$mail=$_POST["mail"];
$subject=$_POST["subject"];
$content=$_POST["content"];
/* 將欄位資料加入資料庫 */
$sql="INSERT guestbook (name,mail,subject,content,putdate)
VALUES ('$name','$mail','$subject','$content',now())";
mysql_query($sql);
?>
==========================================================
4. view.php
<html>
<head>
<title>列出所有留言</title>
</head>
<body>
<a href="board.php">寫寫留言</a><p>
<?php
include("db.php");
/* 查詢欄位資料 */
$sql="select * from guestbook order by no desc"; //從guestbook讀取資料並依no欄位做遞減排序
$result=mysql_query($sql);
/* 顯示資料庫資料 */
while (list($no,$name,$mail,$subject,$content,$putdate)
=mysql_fetch_row($result))
{
echo "留言主題:".$subject;
echo "<br>訪客姓名:".$name;
echo "<br>E-mail:<a href=mailto:$mail>".$mail."</a>";
echo "<br>留言內容:".nl2br($content);
echo "<br>留言時間:".$putdate;
echo "<hr>";
}
echo "共".mysql_num_rows($result)."筆留言";
?>
</body>
</html>