PHP-简单的留言薄功能

index.php

  1 <?php            
  2         error_reporting(0);                                         //关闭NOTICE提示
  3         require_once "conn.php";                     
  4         $pagesize=5;                                                //每页显示5条数据
  5         $sql="select count(*) from guestlist ";                     //选择数据库,计算符合条件的行数并返回行数
  6         $result=  mysql_query($sql);                                //执行,如果成功则返回结果集(从数据库中找到所有的数据,返回条数)
  7         $row = mysql_fetch_row($result);                            //获得数组 Array[0]="数据库里的总条数"
  8         $infoCount =$row[0];                                        //获得总条数:取得数组中的值$row[0]="数据库里的总条数"                                                                                            
  9         $pageCount =  ceil($infoCount/$pagesize);                   //获取总页数(总个数/每页的个数5)
 10         $currpage=empty ($_GET["page"])?1:$_GET["page"];            //如果当前页为空 则定义page=1即$currpage=1反之亦然
 11         if($currpage>$pageCount)                                    //如果输入的页数超过总页数则默认跳转到最后一页
 12         {
 13           $currpage=$pageCount;
 14         }        
 15 ?>
 16 <!DOCTYPE html>
 17 <html>
 18     <head>
 19         <meta charset="utf-8" />
 20         <title></title>
       <!--此处添加了bootstrip样式--> 23 <link href="../dist/css/bootstrap.min.css" type="text/css" rel="stylesheet" /> 24 <link href="css/index.css" type="text/css" rel="stylesheet" /> 28 <script> 29 function test(){ 30 var sum; 31 if(document.frm.title.value==''){ 32 alert('请填写标题'); 33 return false; 34 }else{ 35 sum =document.frm.title.value.length; 36 if(sum<5 || sum>20){ 37 alert('标题长度 5-20个字符'); 38 return false; 39 } 40 } 41 42 if(document.frm.username.value==''){ 43 alert('请填写用户网名'); 44 return false; 45 } 46 47 if(document.frm.content.value==''){ 48 alert("请填写内容"); 49 return false; 50 } 51 return true; 52 } 53 </script> 54 </head> 55 <body> 56 <div class="content"> 57 <h5 style="color: red;"><?php echo $infoCount;?>条留言</h5><br/> 58 <ul class="bt"> 59 <li>留言标题</li> 60 <li>用户网名</li> 61 <li>时间</li> 62 </ul> 63 <?php //从当前页开始 向下取出5个 64 $re= mysql_query("select * from guestlist order by id desc limit ".($currpage-1)*$pagesize.",".$pagesize); 65 while($row= mysql_fetch_assoc($re)) //得到一行数据的数组,再执行则得到再下一行,如果得到是最后一行,那么再执行则返回false 66 { 67 68 ?> 69 <ul class="nr"> 70 <li><?php echo $row["title"];?></li> 71 <li><?php echo $row["username"];?></li> 72 <li><?php echo $row["addtime"];?></li> 73 </ul> 74 <div class="lynr"> 75 <p><strong>留言内容:</strong></p><span><?php echo $row["content"];?></span> 76 </div> 77 <?php 78 } 79 ?> 80 <hr style="width:800px"/> 81 <ul class="pagination"> 82 <!--上一页--> 83 <?php 84 for($i=1;$i<=$pageCount;$i++) 85 { 86 87 if($i==$currpage) 88 { 89 echo "<li><a href=?page=".($i-1).">&laquo;</a></li>"; 90 } 91 92 } 93 ?> 94 <!--数字页--> 95 <?php 96 97 for($i=1;$i<=$pageCount;$i++) 98 { 99 100 if($i==$currpage) 101 { 102 echo "<li ><a style='background-color:#EEEEEE'>$i</a></li>"; 103 }else{ 104 echo "<li><a href='?page=$i'>$i</a></li>";} 105 106 } 107 ?> 108 <!--下一页--> 109 <?php 110 111 for($i=1;$i<$pageCount;$i++) 112 { 113 114 if($i==$currpage) 115 { 116 echo "<li><a href=?page=".($i+1).">&raquo;</a></li>"; 117 } 118 119 } 120 ?> 121 </ul> 122 <br/> 123 <ul> 124 </ul> 125 <hr/> 126 <strong style="color:red">发表留言</strong> 127 <form action="result.php" method="post" name="frm" onsubmit="return test()"> 128 <table cellpadding="0" cellspacing="0" > 129 <tr> 130 <td >留言标题:</td> 131 <td><input type="text" name="title" autocomplete="off"/></td> 132 </tr> 133 <tr> 134 <td>网名:</td> 135 <td><input type="text" name="username" autocomplete="off"/></td> 136 </tr> 137 <tr> 138 <td>留言内容:</td> 139 <td><textarea name="content" cols="42" rows="5" autocomplete="off"/></textarea></td> 140 </tr> 141 <tr> 142 <td></td> 143 <td><input class="btn" type="submit" name="submit" value="提交"/></td> 144 </tr> 145 </table> 146 </form> 147 </div> 148 </body> 149 </html>

 conn.php

 1 <?php
 2 $link = mysql_connect("localhost","root"," ");
 3 mysql_select_db("guestbook");
 4 mysql_query("set names utf-8");
 5 if(!$link){
 6     die("Connection failed: " . mysqli_connect_error());
 7 }
 8     //echo "链接成功";
 9  
10 ?>

result.php

 1 <?php
 2     error_reporting(0);                                     //关闭NOTICE提示
 3     require_once "conn.php";
 4     $title = $_REQUEST['title'];
 5     $username = $_REQUEST['username'];
 6     $content = $_REQUEST['content'];
 7     $content = str_replace("\n","<br>",str_replace(" ","&nbsp;",$content)); //显示'空格'和'回车'
 8     $week = '星期'.mb_substr( "日一二三四五六",date("w"),1,"utf-8" );
    $isok =mysql_query("insert into guestlist(title,username,content,addtime)values('$title','$username','$content','".date("Y-m-d H:i:s")." $week ')"); 9 if($isok) 10 { 11 echo "<script> 12 alert('提交成功'); 13 location.href='index.php'; 14 </script>"; 15 }else { 16 echo "<script> 17 alert('提交失败'); 18 location.href='index.php'; 19 </script>"; 20 } 21 ?>

css/index.css

 1 body{margin:0;padding:0;}
 2 ul,li{list-style: none;margin:0;padding:0;}
 3 a{text-decoration: none;}
 4 .content{
 5     width:800px;
 6     
 7     margin:0 auto;
 8     
 9 }
10 .bt{
11     width:799px;
12     height:20px;
13     text-align: center;
14     background:#EB9316;
15     margin:0 0 5px 0;
16 }
17 .bt>li{
18     float:left;
19     width:265px;
20     height:20px;
21     text-align: center;
22     line-height: 20px;
23     font-size:13px;
24     
25 }
26 .nr{
27     float:left;          /*如果不浮动 后面的lynr会受影响*/
28     width:799px;
29     height:20px;
30     text-align: center;
31     background:#B9DEF0;
32 }
33 .nr>li{
34     float:left;
35     width:265px;
36     height:20px;
37     text-align: center;
38     line-height: 20px;
39     font-size:13px;
40     
41 }
42 .lynr{
43     float:left;                /*如果不浮动会 布局会乱*/
44     width:800px;
45     margin:1px 0 1px 0;
46     
47 }
48 .content p{
49     width:70px;
50     height:50px;
51     float:left;
52     
53     
54 }
55 .content span{
56     display: block;
57     width:710px;
58     float:left;
59     
60     
61 }
62 
63 td{
64     width:80px;
65     padding:5px 0;
66     /*border: 1px solid #79ABFE;*/
67     }
68 td input,textarea{
69     border: 1px solid #79ABFE;
70 }
71 /*tr{
72     display:block;       /*将tr设置为块体元素   显示块状后 就将其包围住了 不是一个矩形了
73    
74    }*/

 dist/css/bootstrap.min.css(自己下载)

效果图

posted on 2017-05-31 10:32  坏人站住  阅读(216)  评论(0编辑  收藏  举报

导航