当前位置:Gxlcms > php框架 > PHP简单留言本功能实现代码

PHP简单留言本功能实现代码

时间:2021-07-01 10:21:17 帮助过:56人阅读

本文实例为大家分享了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>
  21.        <!--此处添加了bootstrip样式-->
  22. <link href="../dist/css/bootstrap.min.css" rel="external nofollow" type="text/css" rel="stylesheet" />
  23. <link href="css/index.css" rel="external nofollow" type="text/css" rel="stylesheet" />
  24. <script>
  25. function test(){
  26. var sum;
  27. if(document.frm.title.value==''){
  28. alert('请填写标题');
  29. return false;
  30. }else{
  31. sum =document.frm.title.value.length;
  32. if(sum<5 || sum>20){
  33. alert('标题长度 5-20个字符');
  34. return false;
  35. }
  36. }
  37. if(document.frm.username.value==''){
  38. alert('请填写用户网名');
  39. return false;
  40. }
  41. if(document.frm.content.value==''){
  42. alert("请填写内容");
  43. return false;
  44. }
  45. return true;
  46. }
  47. </script>
  48. </head>
  49. <body>
  50. <div class="content">
  51. <h5 style="color: red;"><?php echo $infoCount;?>条留言</h5><br/>
  52. <ul class="bt">
  53. <li>留言标题</li>
  54. <li>用户网名</li>
  55. <li>时间</li>
  56. </ul>
  57. <?php //从当前页开始 向下取出5个
  58. $re= mysql_query("select * from guestlist order by id desc limit ".($currpage-1)*$pagesize.",".$pagesize);
  59. while($row= mysql_fetch_assoc($re)) //得到一行数据的数组,再执行则得到再下一行,如果得到是最后一行,那么再执行则返回false
  60. {
  61. ?>
  62. <ul class="nr">
  63. <li><?php echo $row["title"];?></li>
  64. <li><?php echo $row["username"];?></li>
  65. <li><?php echo $row["addtime"];?></li>
  66. </ul>
  67. <div class="lynr">
  68. <p><strong>留言内容:</strong></p><span><?php echo $row["content"];?></span>
  69. </div>
  70. <?php
  71. }
  72. ?>
  73. <hr style="width:800px"/>
  74. <ul class="pagination">
  75. <!--上一页-->
  76. <?php
  77. for($i=1;$i<=$pageCount;$i++)
  78. {
  79. if($i==$currpage)
  80. {
  81. echo "<li><a href=?page=".($i-1).">«</a></li>";
  82. }
  83. }
  84. ?>
  85. <!--数字页-->
  86. <?php
  87. for($i=1;$i<=$pageCount;$i++)
  88. {
  89. if($i==$currpage)
  90. {
  91. echo "<li ><a style='background-color:#EEEEEE'>$i</a></li>";
  92. }else{
  93. echo "<li><a href='?page=$i'>$i</a></li>";}
  94. }
  95. ?>
  96. <!--下一页-->
  97. <?php
  98. for($i=1;$i<$pageCount;$i++)
  99. {
  100. if($i==$currpage)
  101. {
  102. echo "<li><a href=?page=".($i+1).">»</a></li>";
  103. }
  104. }
  105. ?>
  106. </ul>
  107. <br/>
  108. <ul>
  109. </ul>
  110. <hr/>
  111. <strong style="color:red">发表留言</strong>
  112. <form action="result.php" method="post" name="frm" onsubmit="return test()">
  113. <table cellpadding="0" cellspacing="0" >
  114. <tr>
  115. <td >留言标题:</td>
  116. <td><input type="text" name="title" autocomplete="off"/></td>
  117. </tr>
  118. <tr>
  119. <td>网名:</td>
  120. <td><input type="text" name="username" autocomplete="off"/></td>
  121. </tr>
  122. <tr>
  123. <td>留言内容:</td>
  124. <td><textarea name="content" cols="42" rows="5" autocomplete="off"/></textarea></td>
  125. </tr>
  126. <tr>
  127. <td></td>
  128. <td><input class="btn" type="submit" name="submit" value="提交"/></td>
  129. </tr>
  130. </table>
  131. </form>
  132. </div>
  133. </body>
  134. </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. ?>

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(" "," ",$content)); //显示'空格'和'回车'
  8. $week = '星期'.mb_substr( "日一二三四五六",date("w"),1,"utf-8" );
  9.     $isok =mysql_query("insert into guestlist(title,username,content,addtime)values('$title','$username','$content','".date("Y-m-d H:i:s")." $week ')");
  10. if($isok)
  11. {
  12. echo "<script>
  13. alert('提交成功');
  14. location.href='index.php';
  15. </script>";
  16. }else {
  17. echo "<script>
  18. alert('提交失败');
  19. location.href='index.php';
  20. </script>";
  21. }
  22. ?>

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

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

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

人气教程排行