PHP之留言板程序

出于对PHP的好奇和对自己当前工作内容感到烦躁,于前天正式学习PHP。

总的来说,PHP还是比较容易入门,感觉用起来比JSP更轻松,而且学起来也很舒服。

今天做了一个DEMO------留言板程序。做为自己PHP的处女之作。。

一、数据库

 

 

二、源程序

header.php

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <meta http-equiv="Content-Type" content="text/html; charset=gbk">
  3. <table width="930" border="0" align="center" cellspacing="0">
  4.   <tr>
  5.     <td align="center"><H1>留 言 板 程 序</H1></td>
  6.   </tr>
  7.   <tr>
  8.     <td align="center"><hr></td>
  9.   </tr>
  10. </table>

foot.php

 

 

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <meta http-equiv="Content-Type" content="text/html; charset=gbk">
  3. <br>
  4. <script language="javascript">
  5.     function check()
  6.     {
  7.         if(document.message.username.value==""){
  8.             alert("用户名不能为空!");
  9.             return false;
  10.         }
  11.         if(document.message.email.value==""){
  12.             alert("E-Mail不能为空!");
  13.             return false;
  14.         }
  15.         if(document.message.title.value==""){
  16.             alert("标题不能为空!");
  17.             return false;
  18.         }
  19.         if(document.message.content.value==""){
  20.             alert("留言不能为空!");
  21.             return false;
  22.         }
  23.     }
  24. </script>

 

index.php

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gbk">
  5. <title>PHP留言板程序</title>
  6. </head>
  7. <body>
  8. <?php 
  9.     require("header.php");
  10. ?>
  11. <table width="930" border="0" align="center" cellspacing="0" bgcolor="#CCCCCC">
  12.   <tr>
  13.     <td><table width="100%" border="0" align="center" cellspacing="0" bgcolor="#FFFFFF">
  14.       <?php 
  15.     //接收参数;
  16.     $start=$_GET["start"];
  17.     //连接数据库;
  18.     $conn=mysql_connect("localhost:3306","root","root");
  19.     //选择数据库;
  20.     mysql_select_db("messageboard",$conn);
  21.     //操作数据库;
  22.     mysql_query("set names gbk");
  23.     $result=mysql_query("select * from message where isPublic='Y' order by publish_time desc",$conn);
  24.     //取得总的可公开的留言数
  25.     $rowCount=mysql_num_rows($result);
  26.     //取得当前要显示的留言:
  27.     $messages=mysql_query("select * from ( select * from message where isPublic='Y' order by publish_time desc ) B limit ".$start*10 . ",10 ",$conn);
  28.     $no=($start*10+1);
  29.     while($row=mysql_fetch_array($messages))
  30.     {
  31. ?>
  32.       <tr bordercolor="#CCCCCC" bgcolor="#CCCCCC">
  33.         <td><?php  echo $no++ ?>  标题:
  34.             <?php  echo $row["title"] ?></td>
  35.         <td width="20%">用户名: <?php echo $row["name"]?></td>
  36.         <td width="30%">留言时间: <?php echo $row["publish_time"]?></td>
  37.       </tr>
  38.       <tr>
  39.         
  40.         <td colspan="3"><table width="100%"  border="0" cellspacing="0">
  41.           <tr>
  42.             <td width="80" valign="top">留言内容:</td>
  43.             <td><?php echo $row["content"]?></td>
  44.           </tr>
  45.         </table></td>
  46.       </tr>
  47.       <?php 
  48.         
  49.     }
  50. ?>
  51.      <tr>
  52.         <td colspan="3"><hr align="center" width="100%" size="1"></td>
  53.       </tr>
  54.       <tr align="center">
  55.         <td colspan="3"><a href="index.php?start=0">首页</a>    
  56.             <?php 
  57.     if($start<=0){
  58. ?>
  59.       上一页
  60.       <?php 
  61.     }else
  62.     {
  63. ?>
  64.       <a href="index.php?start=<?php echo ($start-1) ?>"">上一页</a>
  65.       <?php
  66.     }
  67. ?>
  68.     
  69.       <?php
  70.     if($start>=floor($rowCount/10))
  71.     {
  72. ?>
  73.       下一页
  74.       <?php
  75.     }else
  76.     {
  77. ?>
  78.       <a href="index.php?start=<?php echo ($start+1) ?>">下一页</a>
  79.       <?php
  80.     }
  81. ?>
  82.    <a href="index.php?start=<?php echo floor($rowCount/10) ?>"> 尾页</a>    第<?php echo ($start+1) ?>/<?php echo (floor($rowCount/10)+1)?>页
  83. </td>
  84.       </tr>
  85.     </table></td>
  86.   </tr>
  87. </table>
  88. <?php 
  89.     require("foot.php");
  90.     mysql_close($conn);
  91. ?>
  92. </body>
  93. </html>

 

addMessage.php

 

  1. <?php
  2.     //接收参数;
  3.     $name=$_POST["username"];
  4.     $email=$_POST["email"];
  5.     $title=$_POST["title"]; 
  6.     $content=$_POST["content"];
  7.     $isPublic=$_POST["isPublic"];
  8.     $date=getdate(date("U"));
  9.     $publish_time=$date["year"]."-".$date["mon"]."-".$date["mday"]." ".$date["hours"].":".$date["minutes"].":".$date["seconds"];
  10.     //echo $publish_time;
  11.     //DB connect
  12.     $conn=mysql_connect("localhost:3306","root","root");
  13.     if(!$conn)
  14.     {
  15.         die("DB Connect Error:".mysql_error());
  16.     }
  17.     //select DB
  18.     $mysql_select=mysql_select_db("messageboard",$conn);
  19.     if(!$mysql_select)
  20.     {
  21.         die("Select DB Error:".mysql_error());
  22.     }
  23.     //Operation
  24.     mysql_query("set names gbk");
  25.     if(mysql_query("insert into message(title,content,name,email,publish_time,active,isPublic) values('".$title."','".$content."','".$name."','".$email."','".$publish_time."','"."Y"."','".$isPublic."')")){
  26.         //success       
  27. ?>
  28.         <script language="javascript">
  29.             //alert("<?php echo $publish_time ?>");
  30.             document.location.href="index.php";
  31.         </script>
  32. <?php
  33.     }else{
  34.         //failure
  35.         echo "insert into message(title,content,name,email,publish_time,active,isPublic) values('".$title."','".$content."','".$name."','".$email."','".$publish_time."','"."Y"."','".$isPublic."')";
  36. ?>
  37.         <script language="javascript">
  38.             alert("留言失败!");
  39.             //document.location.href="index.php";
  40.         </script>
  41. <?php
  42.     }
  43.     //Close
  44.     mysql_close($conn);
  45. ?>

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值