温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用PHP怎么实现一个留言板功能

发布时间:2021-04-16 16:32:56 来源:亿速云 阅读:202 作者:Leah 栏目:开发技术

使用PHP怎么实现一个留言板功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

user表

使用PHP怎么实现一个留言板功能
friend表
使用PHP怎么实现一个留言板功能
text表
使用PHP怎么实现一个留言板功能

#首先需要写一个注册与登录

##注册
zhuce.html

<meta charset="utf-8"> <title>zhuce</title> </head> <body> <form method="POST" action="zhuce.php"> <div > <h2>用户注册页面</h2> 用户名:<input type="text" name="username"> <div>密&nbsp;&nbsp;&nbsp;码:<input type="password" name="password"> <div><input type="submit" name="submit" value="注册"></div> </div> </form> </body>

zhuce.php

<?php session_start(); header("Content-type: text/html; charset=utf-8"); //处理数据库用户名乱码 $user=$_POST["username"]; $pwd=$_POST["password"]; if($user==""||$pwd=="")	 {    	echo "<script>alert('请确认信息完整性!'); history.go(-1);</script>"; 	 } else    	{	   	$link=mysqli_connect("localhost","root","","liuyan");//连接数据库	 	mysqli_query($link,"set names utf8"); 	 	$sql="select username from user where username='$_POST[username]'";     	$result=mysqli_query($link,$sql);//执行sql语句      	$num=mysqli_num_rows($result);//统计执行结果影响的行数      	if($num)//如果存在该用户      	{      	echo "<script>alert('用户名已存在!'); history.go(-1);</script>";      	}       else//注册新用户     	 {       	$sql_insert="insert into user (username,password)values('$_POST[username]','$_POST[password]')";      	$res_insert=mysqli_query($link,$sql_insert);      	if($res_insert)      	{      	echo "<script>alert('注册成功!');window.location='denglu.html';</script>";       	}      	else      	{      	echo "<script>alert('系统繁忙请重试!'); history.go(-1);</script>";       	}      	  }   }  ?>

#效果如下
使用PHP怎么实现一个留言板功能

##登录
denglu.html

<head> <meta charset="utf-8"> <title>denglu</title> </head> <body> <form method="POST" action="denglu.php"> <div > <h2>用户登录页面</h2> 用户名:<input type="text" name="username"> <div>密&nbsp;&nbsp;&nbsp;码:<input type="password" name="password"></div><br/> <input type="submit" name="submit" value="登录">  <a href="zhuce.html" rel="external nofollow" >注册</a>   </div> </form> </body>

denglu.php

<?php   session_start();   	$user=$_POST["username"];	$_SESSION["uesrname"]=$user;//session超全局变量	$pwd=$_POST["password"];//获取密码	if($user=""||$pwd="")	{    echo "<script>alert('请输入用户名或密码!'); history.go(-1);</script>"; 	}	else	{	$link=mysqli_connect("localhost","root","","liuyan");//连接数据库     mysqli_query($link,"set names utf8");      $sql = "select username,password from user where username = '$_POST[username]' and password = '$_POST[password]'"; 	$result=mysqli_query($link,$sql);//执行sql语句	$num=mysqli_num_rows($result);//统计影响结果行数,作为判断条件	if($num)	{	echo "<script>alert('登录成功!');window.location='003.php';</script>";//登录成功页面跳转 	}	else	{	echo "<script>alert('用户名或密码不正确!');history.go(-1);</script>"; 	  }	} ?>

#效果如下
使用PHP怎么实现一个留言板功能
#下面需要我们写一个登录进去的页面,它显示别人给你发来的留言以及进入你要给别人留言的页面或者退出系统,在这里我将html代码嵌入php
php代码如下
003.php

<?php session_start(); global $user;//定义$user global $user1; $_SESSION["username"]=$user; $username=$_SESSION["uesrname"]; $user1=implode("",$_SESSION);//将session中的数组变为字符串元素 $link=mysqli_connect("localhost","root","","liuyan");//连接数据库 mysqli_query($link,"set names utf8");  $sql="select * from text where recever='{$username}'"; $result=mysqli_query($link,$sql);//执行语句 if($num=mysqli_num_rows($result))//将HTML嵌入PHP中,实现从数据库中获得留言信息 {?>	<!DOCTYPE html> <html> <head>  <meta charset="utf-8"> </head>  <body>  <div>  	<a href="fabu.html" rel="external nofollow" >发布信息</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  	<a href="tuichu.php" rel="external nofollow" >退出系统</a></h4></div>  	<br/><br/>    	<h3>留言信息:</h3>  	<table cellpadding="0" cellspacing="0" border="1" width="60%"> <tr bgcolor="#8BBCC7">	<td>发送人</td>	<td>接收人</td>	<td>发送时间</td>	<td>信息内容</td>	<?php echo '<pre>';	while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))	{?>	<tr bgcolor="#FFFFFF">	<td ><?php echo $row['sender'];?>&nbsp;</td>	<td >&nbsp;<?php echo $row['recever'];?>&nbsp;</td>	<td >&nbsp;<?php echo $row['comment'];?>&nbsp;</td>	<td >&nbsp;<?php echo $row['time'];?>&nbsp;</td>   <?php      }     ?> </tr> </table> </body> </html>    <?php }?>

#效果如下
使用PHP怎么实现一个留言板功能
#接下来我们就该写发布的页面以及PHP代码了
fabu.html

<head>	<meta charset="utf-8">	<title>fabu</title> </head> <body>  <form method="POST" action="fabu.php"> <h2>发布信息<h2> <a href="003.php" rel="external nofollow" >主页面</a> <div>接收人:<input type="text" name="recever"> </select> </div> </select> </div> <br/> <div>	信息内容:<textarea input type="text" name="neirong"></textarea> </div><br/> <input type="submit" value="发送"> </form>

fabu.php

<?php session_start(); header("Content-type: text/html; charset=utf-8"); global $user; $re=$_POST["recever"];//获取recever $comment=$_POST["neirong"];//获取留言 @date_default_timezone_set(PRC);//将数组变为字符串函数 $time=date("Y-m-d G:i:s");//获取时间,G为24小时制 $_SESSION["username"]=$user;//开启session $user1=implode("",$_SESSION);//将数组转为字符串 $link=mysqli_connect("localhost","root","","liuyan");//连接数据库 mysqli_query($link,"set names utf8");  $sql="insert into text(sender,recever,comment,time) values('$user1','$re','$comment','$time')"; $result=mysqli_query($link,$sql);//执行语句 $sql1="insert into friend(me,friend) values('$user1','$re')";//将me,friend存入数据库 $result=mysqli_query($link,$sql1);//执行语句 if($recever=""||$comment="") {	echo "<script>alert('发布失败!');window.location='fabu.html';</script>"; } else {	echo "<script>alert('发布成功!');window.location='fabu.html';</script>"; } ?>

#效果如下
使用PHP怎么实现一个留言板功能
#最后是退出系统

<?php session_start(); unset($_SESSION["uesrname"]); echo "<script>alert('退出成功!');window.location='denglu.html';</script>";  ?>

看完上述内容,你们掌握使用PHP怎么实现一个留言板功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI