作为一个php的初学者,我试着写了一个留言板,页面有点丑,多多见谅,嘻嘻嘻
我们写留言板需要用到数据库,所以我们先要建立三个表
user表
friend表
text表
首先需要写一个注册与登录
注册
zhuce.html
<meta charset="utf-8"><title>zhuce</title></head><body><form method="post" action="zhuce.php"><p style="margin-left: 500px;margin-top:200px;height: 250px;width: 250px"><h1>用户注册页面</h1>用户名:<input type="text" name="username"><p>密 码:<input type="password" name="password"><p><input type="submit" name="submit" value="注册"></p></p></form></body>
zhuce.php
<?phpsession_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>; } } } ?>
效果如下
登录
denglu.html
<head><meta charset="utf-8"><title>denglu</title></head><body><form method="post" action="denglu.php"><p style="margin-left: 500px;margin-top:200px;height: 250px;width: 250px"><h1>用户登录页面</h1>用户名:<input type="text" name="username"><p>密 码:<input type="password" name="password"></p><br/><input type="submit" name="submit" value="登录"> <a href="zhuce.html">注册</a> </p></form></body>
denglu.php
效果如下
下面需要我们写一个登录进去的页面,它显示别人给你发来的留言以及进入你要给别人留言的页面或者退出系统,在这里我将html代码嵌入php
php代码如下
003.php
<?phpsession_start();global $user;//定义$userglobal $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> <p> <a href="fabu.html">发布信息</a> <a href="tuichu.php">退出系统</a></h3></p> <br/><br/> <h2>留言信息:</h2> <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'];?> </td> <td > <?php echo $row['recever'];?> </td> <td > <?php echo $row['comment'];?> </td> <td > <?php echo $row['time'];?> </td> <?php } ?></tr></table></body></html> <?php }?>
效果如下
#接下来我们就该写发布的页面以及php代码了
fabu.html
<head> <meta charset="utf-8"> <title>fabu</title></head><body> <form method="post" action="fabu.php"><h1>发布信息<h1><a href="003.php">主页面</a><p>接收人:<input type="text" name="recever"></select></p></select></p><br/><p> 信息内容:<textarea input type="text" name="neirong"></textarea></p><br/><input type="submit" value="发送"></form>
fabu.php
效果如下
最后是退出系统
效果如下
第一次写博客,写得不好多有见谅,希望对大家学习php有所帮助,如有疑问或者改进方法请留言。
更多相关技术文章,请访问!