出于对PHP的好奇和对自己当前工作内容感到烦躁,于前天正式学习PHP。
总的来说,PHP还是比较容易入门,感觉用起来比JSP更轻松,而且学起来也很舒服。
今天做了一个DEMO------留言板程序。做为自己PHP的处女之作。。
一、数据库
二、源程序
header.php
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <meta http-equiv="Content-Type" content="text/html; charset=gbk">
- <table width="930" border="0" align="center" cellspacing="0">
- <tr>
- <td align="center"><H1>留 言 板 程 序</H1></td>
- </tr>
- <tr>
- <td align="center"><hr></td>
- </tr>
- </table>
foot.php
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <meta http-equiv="Content-Type" content="text/html; charset=gbk">
- <br>
- <script language="javascript">
- function check()
- {
- if(document.message.username.value==""){
- alert("用户名不能为空!");
- return false;
- }
- if(document.message.email.value==""){
- alert("E-Mail不能为空!");
- return false;
- }
- if(document.message.title.value==""){
- alert("标题不能为空!");
- return false;
- }
- if(document.message.content.value==""){
- alert("留言不能为空!");
- return false;
- }
- }
- </script>
index.php
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gbk">
- <title>PHP留言板程序</title>
- </head>
- <body>
- <?php
- require("header.php");
- ?>
- <table width="930" border="0" align="center" cellspacing="0" bgcolor="#CCCCCC">
- <tr>
- <td><table width="100%" border="0" align="center" cellspacing="0" bgcolor="#FFFFFF">
- <?php
- //接收参数;
- $start=$_GET["start"];
- //连接数据库;
- $conn=mysql_connect("localhost:3306","root","root");
- //选择数据库;
- mysql_select_db("messageboard",$conn);
- //操作数据库;
- mysql_query("set names gbk");
- $result=mysql_query("select * from message where isPublic='Y' order by publish_time desc",$conn);
- //取得总的可公开的留言数
- $rowCount=mysql_num_rows($result);
- //取得当前要显示的留言:
- $messages=mysql_query("select * from ( select * from message where isPublic='Y' order by publish_time desc ) B limit ".$start*10 . ",10 ",$conn);
- $no=($start*10+1);
- while($row=mysql_fetch_array($messages))
- {
- ?>
- <tr bordercolor="#CCCCCC" bgcolor="#CCCCCC">
- <td><?php echo $no++ ?> 标题:
- <?php echo $row["title"] ?></td>
- <td width="20%">用户名: <?php echo $row["name"]?></td>
- <td width="30%">留言时间: <?php echo $row["publish_time"]?></td>
- </tr>
- <tr>
- <td colspan="3"><table width="100%" border="0" cellspacing="0">
- <tr>
- <td width="80" valign="top">留言内容:</td>
- <td><?php echo $row["content"]?></td>
- </tr>
- </table></td>
- </tr>
- <?php
- }
- ?>
- <tr>
- <td colspan="3"><hr align="center" width="100%" size="1"></td>
- </tr>
- <tr align="center">
- <td colspan="3"><a href="index.php?start=0">首页</a>
- <?php
- if($start<=0){
- ?>
- 上一页
- <?php
- }else
- {
- ?>
- <a href="index.php?start=<?php echo ($start-1) ?>"">上一页</a>
- <?php
- }
- ?>
- <?php
- if($start>=floor($rowCount/10))
- {
- ?>
- 下一页
- <?php
- }else
- {
- ?>
- <a href="index.php?start=<?php echo ($start+1) ?>">下一页</a>
- <?php
- }
- ?>
- <a href="index.php?start=<?php echo floor($rowCount/10) ?>"> 尾页</a> 第<?php echo ($start+1) ?>/<?php echo (floor($rowCount/10)+1)?>页
- </td>
- </tr>
- </table></td>
- </tr>
- </table>
- <?php
- require("foot.php");
- mysql_close($conn);
- ?>
- </body>
- </html>
addMessage.php
- <?php
- //接收参数;
- $name=$_POST["username"];
- $email=$_POST["email"];
- $title=$_POST["title"];
- $content=$_POST["content"];
- $isPublic=$_POST["isPublic"];
- $date=getdate(date("U"));
- $publish_time=$date["year"]."-".$date["mon"]."-".$date["mday"]." ".$date["hours"].":".$date["minutes"].":".$date["seconds"];
- //echo $publish_time;
- //DB connect
- $conn=mysql_connect("localhost:3306","root","root");
- if(!$conn)
- {
- die("DB Connect Error:".mysql_error());
- }
- //select DB
- $mysql_select=mysql_select_db("messageboard",$conn);
- if(!$mysql_select)
- {
- die("Select DB Error:".mysql_error());
- }
- //Operation
- mysql_query("set names gbk");
- if(mysql_query("insert into message(title,content,name,email,publish_time,active,isPublic) values('".$title."','".$content."','".$name."','".$email."','".$publish_time."','"."Y"."','".$isPublic."')")){
- //success
- ?>
- <script language="javascript">
- //alert("<?php echo $publish_time ?>");
- document.location.href="index.php";
- </script>
- <?php
- }else{
- //failure
- echo "insert into message(title,content,name,email,publish_time,active,isPublic) values('".$title."','".$content."','".$name."','".$email."','".$publish_time."','"."Y"."','".$isPublic."')";
- ?>
- <script language="javascript">
- alert("留言失败!");
- //document.location.href="index.php";
- </script>
- <?php
- }
- //Close
- mysql_close($conn);
- ?>