首页 > 后端开发 > php教程 > php实现简单的留言板功能(附源码)

php实现简单的留言板功能(附源码)

烟雨青岚
发布: 2023-04-08 19:58:01
转载
11022 人浏览过

php实现简单的留言板功能(附源码)

php实现简单的留言板功能

1、原理

简单的说就是 数据库的创建,添加数据,显示在前端上。我的程序只是简单的留言再显示。

首先写好留言的前端页面,就简单的写入作者,标题和内容。

2、界面

php实现简单的留言板功能(附源码)

3、显示留言的界面

php实现简单的留言板功能(附源码)

4、代码

(1)添加留言的页面

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

<!DOCTYPE HTML>

    <HTML>

<Head>

    <meta  http-equiv="CONTENT-TYPE" ; content="text/html"  ; charset="UTF-8">

    <title>留言</title>

    <style type="text/css">

     .message{

         margin-top:0px;

     }

     h1{

         margin-top:200px;

     }

    </style>

</Head>

<Body>

   <h1 align="center">留言板</h1>

   <div class="message">

       <form name="addform" id="addform" method="post" action="message_handle.php">

           <table type="text" align="center" border="1px,solid">

               <input type="hidden" id="id" name="id" />

            <tr>

               <td>标题</td>

               <td><input type="text" name="title" id="title"/></td>

            </tr>

            <tr>

                <td>作者</td>

                <td><input type="text" name="author" id="author"/> </td>

            </tr>

            <tr>

                <td>内容</td>

                <td><textarea name="message" id="message" cols="60" role="15"></textarea></td>

            </tr>

            <tr>

                <td><input type="submit" name="sumbit"/></td>

                <td><input type="reset" name="reset"/></td>

            </tr>

           </table>

       </form>

   </div>

</Body>

</HTML>

登录后复制

(2)留言的后台处理,把作者,标题,内容存入建好的数据库中

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

<?php

header("CONTENT-TYPE:text/html;charset=UTF-8");

define("HOST","127.0.0.1");

define("USERNAME","root");

define("PASSWORD","");

if($con=new mysqli(HOST,USERNAME,PASSWORD,"test")){

    echo $con->error;

}

if($con->select_db("messageboard")){

    echo $con->error;

}

if($con->query("SET NAMES utf8")){

    echo $con->error;

}

$id=$_POST["id"];

$title=$_POST["title"];

$author=$_POST["author"];

$message=$_POST["message"];

$time=date(&#39;y-m-d h:m:s&#39;);

$sql="insert into messageboard(id,title,author,message,dateline) values(&#39;$id&#39;,&#39;$title&#39;,&#39;$author&#39;,&#39;$message&#39;,&#39;$time&#39;)";

if($str=$con->query($sql)){

    echo "<script>alert(&#39;留言成功&#39;);window.location.href=&#39;show_message.php&#39;</script>";

}

else {

    echo "<script>alert(&#39;留言失败&#39;);window.location.href=&#39;messageboard.php&#39;</script>";

}

?>

登录后复制

(3)下面是显示留言的页面代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

<?php

header("CONTENT-TYPE:text/html;charset=UTF-8");

define("HOST","127.0.0.1");

define("USERNAME","root");

define("PASSWORD","");

if($con=new mysqli(HOST,USERNAME,PASSWORD,"test")){

    echo $con->error;

}

if($con->select_db("messageboard")){

    echo $con->error;

}

if($con->query("SET NAMES utf8")){

    echo $con->error;

}

$sql="select * from messageboard ORDER BY dateline DESC ";

$str=$con->query($sql);

if($str && mysqli_num_rows($str)){

    while($row= mysqli_fetch_assoc($str)){

        $data[]=$row;

    }

}

?>

<!DOCTYPE HTML>

<HTML>

<Head>

    <meta  http-equiv="CONTENT-TYPE" ; content="text/html"  ; charset="UTF-8">

    <title>留言板</title>

    <style type="text/css">

    </style>

</Head>

<Body>

<div>

    <?php

    if(empty($data)){

        echo "当前没有留言";

    }

    else{

    foreach($data as $value) {

    ?>

    <table cellpadding="2" cellspacing="8" align="center" border="1px,solid">

        <tr>

            <td>标题</td>

            <td><?php echo $value[&#39;title&#39;]; ?></td>

        </tr>

        <tr>

            <td>作者</td>

            <td><?php echo $value[&#39;author&#39;]; ?></td>

        </tr>

        <tr>

            <td>内容</td>

            <td><?php echo $value[&#39;message&#39;]; ?></td>

        </tr>

        <tr>

            <td><?php echo $value[&#39;dateline&#39;];;?></td>

        </tr>

    </table>

</div>

<?php

 }

}

?>

</Body>

</HTML>

登录后复制

5、所遇到的问题

 刚开始显示页面上不能显示数据,找了半天原因,结果是因为在sql中写错了查询方式写成了:

1

select * from message where dateline desc;

登录后复制

用where得有条件,才能查询到。得有例如:

1

select * from message where dateline=$date;

登录后复制

因为我的程序没有从前个页面传递数据到这,所以只能用下面这种通过时间来排序罗列出所有数据。

1

select * from message order by dateline;

登录后复制

感谢大家的阅读,以上代码有不足的地方请大家指出,希望大家可以有所收获。

本文转载自:https://blog.csdn.net/jeak2015/article/details/53440522

推荐教程:《PHP教程

以上是php实现简单的留言板功能(附源码)的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板