conn.php檔案
<?php $conn = @ mysql_connect("localhost", "root", "123456789") or die("数据库链接错误"); mysql_select_db("bbs", $conn); mysql_query("set names 'utf8'"); //使用utf-8中文编码; ?>
#add.html檔案
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link href="css.css" rel="stylesheet" type="text/css"> <title>Title</title> <?php include ("add.php")?> </head> <script> function CheckPost() { if(myform.user.value=="") { alert("请填写用户"); myform.user.focus(); return false; } if (myform.title.value.length<5) { alert("标题不能少于5个字符"); myform.title.focus(); return false; } if (myform.content.value=="") { alert("内容不能为空"); myform.content.focus(); return false; } } </script> <body> <b> <a href="list.php">浏览留言</a> </b> <hr size=1> <form action="add.php" method="post" name="myform" onsubmit="return CheckPost();"> 用户:<input type="text" size="10" name="user"/><br> 标题:<input type="text" name="title" /><br> 内容:<textarea name="content"></textarea><br> <input type="submit" name="submit" value="发布留言" /> </form> </body> </html>
css.css檔案
td { line-height: 16pt; font-size: 10pt; font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; } a:link { text-decoration: none; color: #000000; } body { font-size: 10pt; line-height: 13pt; background-color: #ECF5FF; } textarea { font-size: 8pt; font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; border: 1px solid #999999; padding: 5px; } form { margin: 0px; padding: 0px; } .textdrow { color:#666666; filter: DropShadow(Color=white, OffX=1, OffY=1, Positive=1); } .p { text-indent: 24px; }
add.php
<?php
include ("conn.php");
if ($_POST['submit']){
$sql="insert into message(id,user,title,content,lastdate) ".
"values ('','$_POST[user]','$_POST[title]','$_POST[content]',now())";
mysql_query($sql);
echo "<script>alert('添加成功');history.go(-1)</script>";
}
?>
#list.php檔案
<!DOCTYPE html> <html lang="utf-8"> <head> <link href="css.css" rel="stylesheet" type="text/css"> </head> <?php include ("conn.php"); ?> <table width=500 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef" > <?php $sql="select * from message order by id desc"; $query=mysql_query($sql); while($row=mysql_fetch_array($query)){ ?> <tr bgcolor="#eff3ff"> <td><font color="red">标题:</font><?php echo $row['title'];?> <font color="red">用户:</font><?php echo $row['user'];?><div align="right"><a href="del.php?id=<?php echo $row['id'];?>">删除</a></div></td> </tr> <tr bgColor="#ffffff"> <td>发表内容:<?php echo $row['content'];?></td> </tr> <tr bgColor="#ffffff"> <td><div align="right">时间:<?php echo $row['lastdate'];?></td> </tr> <?php } ?> <tr bgcolor="#f0fff0"> <td><div align="right"><a href="add.html">返回留言</a> </td> </tr> </table> </html>######################del.php檔案#########
<?php include 'conn.php'; $id = $_GET['id']; $query="delete from message where id=".$id; mysql_query($query); ?> <?php //页面跳转,实现方式为javascript $url = "list.php"; echo "<script>"; echo "window.location.href='$url'"; echo "</script>"; ?>###至此留言板的編寫基本上完成,本篇只起到拋磚引玉,作為最簡單最基本的實例,讓讀者先了解如何使用php來操作資料庫,組合成簡單的實例。 ###