今天小编就为大家带来一篇PHP实现留言板功能的文章。小编觉得挺实用的,为此分享给大家做个参考。一起跟随小编过来看看吧。

首先创建消息表,其主要字段有发送者的名称,消息内容,以及消息发送时间;

SQL:

CREATE TABLE `guanhui`.`message` ( `id` INT(10) NOT NULL AUTO_INCREMENT COMMENT '消息ID' , `sender` VARCHAR(60) NOT NULL COMMENT '发送者' , `content` TEXT NOT NULL COMMENT '消息内容' , `send_time` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT '发送时间' , PRIMARY KEY (`id`)) ENGINE = MyISAM;

然后在前端创建表单并将留言消息查询出来,进行列表展示;

表单HMTL:

<form action="./send_message.php" method="POST"> <input type="text" name="sender" placeholder="你的昵称"> <textarea rows="4" name="content" placeholder="留言内容"></textarea> <button type="submit">发送</button></form>

展示列表:

<?php//链接数据库$conn = mysql_connect("loclhost:3306","root","root"); //判断错误函数if(!$conn){ die(mysql_error());}//选择数据库mysql_query("use message",$conn);//设定字符集编码mysql_query("set names utf8",$conn);//查询语句$sql = "select * from message";//执行语句$res = mysql_query($sql);//建立一个空数组$data = array();//执行循环while($row = mysql_fetch_assoc($res)){ $data[] = $row;}?><table> <tr> <th>ID</th> <th>Name</th> <th>Sender</th> <th class="content">Content</th> <th>操作</th> </tr> <?php foreach($data as $k=>$v){?> <tr> <td><?=$v['id']?></td> <td><?=$v['name']?></td> <td><?=$v['sender']?></td> <td><?=$v['content']?></td> <td> <a href="./update.php?id=<?=$v['id']?>">修改</a> <a href="./del.php?id=<?=$v['id']?>">删除</a> </td> </tr> <?php }?><table>

最后将表单提交过来的信息保存到数据库即可。

<?php//链接数据库$conn = mysql_connect("loclhost:3306","root","root"); //判断错误函数if(!$conn){ die(mysql_error());}//选择数据库mysql_query("use message",$conn);//设定字符集编码mysql_query("set names utf8",$conn); //获取表单值$name = $_POST['name'];$sender = $_POST['sender'];$content =$_POST['content'];//插入数据库语句$sql = "insert into message(name,sender,content)values('$name','$sender','$content')";//执行数据$res = mysql_query($sql);//判断结果if($res){ echo "增加成功";}else{ die("增加失败".mysql_error());}

以上就是PHP实现留言板功能的详细内容,代码示例简单明了,如果在日常工作遇到此问题。通过这篇文章,希望你能有所收获,更多详情敬请关注亿速云行业资讯频道!