步骤:
1、设计数据库
2、编写连接数据库文件
3、编写“增”的功能
4、查看的功能
数据库bbs
create table message(
id int(4) auto_increment PRIMARY KEY,
user char(10) not null,
content char(100) not null,
time date not null
)
1.head.php
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>简易留言板</title>
<link href="images/css.css" rel="stylesheet" type="text/css">
<b><a href="add.php">添加留言</a> | <a href="list.php">浏览留言</a></b>
<hr size=1>
2.conn.php
<?php
$conn= @mysql_connect("localhost","root","123456") or die("数据库连接失败");
mysql_select_db("bbs",$conn);
function htmtocode($str){
$str=str_replace(" ", " ", str_replace("\n", "<br>", $str));
return $str;
}
?>
3.add.php
<?php
include 'conn.php';
include 'head.php';
if($_POST[submit]){
$sql="insert into message values(null,'$_POST[user]','$_POST[content]',now())";
$query=mysql_query($sql,$conn) or die(mysql_error());
if($query!=0){
echo "<script language=\"javascript\"> alert('提交成功'); location.href='list.php' </script>";
}
}
?>
<script language=javascript>
function CheckPost(){
if(myform.user.value==""){
alert("请填写用户名");
myform.user.focus();
return false;
}
if(myform.content.value==""){
alert("请填写内容");
myform.content.focus();
return false;
}
}
</script>
<form action = "add.php" method ="post" name="myform" onsubmit="return CheckPost()">
用户:<input type ="text" size="15" name="user"><br><br>
内容:<textarea name="content"></textarea><br><br>
<input type="submit" name="submit" value="提交">
<input type="reset" name="reset" value="重置">
</form>
4.list.php
<?php
include 'conn.php';
include 'head.php';
$sql="select * from message order by time desc";
$query=mysql_query($sql);
if($query){
while($row=mysql_fetch_array($query)){
?>
<table width=500 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef">
<tr bgcolor="#eff3ff">
<td>发表时间:<?php echo $row[time]?> 用户:<?php echo $row[user]?></td>
</tr>
<tr bgColor="#ffffff">
<td>内容:<?php echo htmtocode($row[content])?></td>
</tr>
</table>
<?php
}
}else{
echo "查询失败";
}
?>
updata::::::::分页实例
<?php
include 'conn.php';
include 'head.php';
$pagesize=5;
$url=$_SERVER["REQUEST_URI"];
$url=parse_url($url);
$url=$url[path];
$numq=mysql_query("SELECT * FROM message");
$num = mysql_num_rows($numq);
$total=ceil($num/$pagesize);
if($_GET[page]){
$pageval=$_GET[page];
$page=($pageval-1)*$pagesize;
$page.=',';
}
if($num > $pagesize){
if($pageval<=1){
$pageval=1;
echo "共 $total 页 $num 条 第 $pageval 页".
" 上一页 <a href=$url?page=".($pageval+1).">下一页</a>";
}elseif ($pageval==$total){
echo "共 $total 页 $num 条 第 $pageval 页".
" <a href=$url?page=".($pageval-1).">上一页</a> 下一页";
}else{
echo "共 $total 页 $num 条 第 $pageval 页".
" <a href=$url?page=".($pageval-1).">上一页</a> <a href=$url?page=".($pageval+1).">下一页</a>";
}
}
$SQL="SELECT * FROM message order by time desc limit $page $pagesize ";
$query=mysql_query($SQL);
while($row=mysql_fetch_array($query)){
echo "<hr><b>".$row[time]." | ".$row[user]." | ".$row[content];
}
?>