首先寫個簡單版的留言闆(ps:以後慢慢加功能),思路如下:
背景3個php檔案:1.連接配接資料庫(conn.php)2.存入資料庫(addmsg.php)3.讀取資料庫和顯示(showlist.php)。
前台1個檔案:1.留言界面(index.html)
資料庫:bbs
第一步:建個資料庫bbs,建立表message,建立字段。圖如下:
第二步:寫前台界面代碼:
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>liuyanban</title>
</head>
<body>
<form action="addmsg.php" method="post" name = "myform" >
用名:<input type="text" size="10" name="userName" /><br/>
标題:<input type="text" name="title" /><br/>
内容:<textarea name="content" cols="60" rows="9" ></textarea><br/>
<input type="submit" name="submit" value="送出留言" />
</form>
</body>
</html>
第三步寫連接配接資料庫檔案con.php:
<?php
$dbName = "bbs";
$conn = @ mysql_connect("localhost", "root", "12345678") or die("資料庫連結錯誤");
$flag = mysql_select_db($dbName, $conn);
mysql_query("set names 'UTF-8'"); //使用UTF-8中文編碼;
function toHtmlcode($content)
{
return $content = str_replace("\n","<br>",str_replace(" ", " ", $content));
}
?>
第四步寫存入資料庫檔案addmsg.php:
<?php
include("conn.php");
if(@$_POST['submit']){
$sql = "insert into message (id,user,title,content,lastdate)" .
"values ( '','$_POST[userName]','$_POST[title]','$_POST[content]',now())";
mysql_query($sql);
echo "添加成功";
}
?>
第五步寫讀取資料庫檔案并顯示shoulist.php:
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>show</title>
</head>
<body>
<?php
include("conn.php");
?>
<table width=500 align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef">
<?php
$sql = "SELECT * FROM message order by lastdate desc";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)){
?>
<tr bgcolor="#eff3ff">
<td><b><big>
标題:<?= $row['title']?></big><b/> <b><sub>
使用者:<?= $row['user']?></sub></b></td>
</tr>
<tr bgColor="#ffffff">
<td>内容:<?= toHtmlcode($row['content'])?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
好了,到此大功告成,這是超級簡單版,以後慢慢增加功能。轉載請注明出處,謝謝。