


The editor below will bring you an example of using PHP to connect to the database to implement the message board function (recommended). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.
PHP implements the message board function:
1 The first is the login page:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>留言板登录</title> <script src="bootstrap/js/jquery-1.11.2.min.js"></script> <script src="bootstrap/js/bootstrap.min.js"></script> <link href="bootstrap/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/> </head> <style> .header{ margin-left: 550px; margin-top: 150px; height: 300px; max-width: 300px; } .xiugai{ max-width: 200px; } .login{ margin-top: 10px; } </style> <body> <form action="messloginchuli.php" method="post"> <p class="header"> <h2 id="开发部内部留言板">开发部内部留言板</h2> <p class="input-group xiugai"> <span class="input-group-addon" >用户名:</span> <input type="text" class="form-control" name="uid" placeholder="请输入用户名"> </p> <p class="input-group xiugai" > <span class="input-group-addon">口令:</span> <input type="text" class="form-control" name="pwd" placeholder="请输入口令"> </p> <button type="submit" class="btn btn-success login">登录</button> </p> </form> </body> </html>
2 After the login page is completed, you need to enter the login processing page, which is the messloginchuli.php submitted above
<?php session_start(); // 登录之后要把所包含登录的页面连接起来,开启session $uid = $_POST["uid"]; $pwd = $_POST["pwd"]; require_once "./DBDA.class.php"; $db = new DBDA(); $sql = "select password from yuangong where username='{$uid}'"; $arr = $db->query($sql,0); //var_dump($arr[0][0]); if($arr[0][0]=$pwd && !empty($pwd)){ $_SESSION["uid"]=$uid; header("location:message.php"); } ?>
The login page looks like this:
3. After the login is completed, you will enter the home page. The following is the table of the designed database and the code of the main page:
##
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="bootstrap/js/jquery-1.11.2.min.js"></script> <script src="bootstrap/js/bootstrap.min.js"></script> <link href="bootstrap/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/> </head> <style> .mess{ max-width: 800px; margin-left: 250px; margin-top: 150px; } </style> <body> <?php session_start(); $uid = $_SESSION["uid"]; if(empty($_SESSION["uid"])){ header("location:messlogin.php"); exit; } ?> <p > <a href="publish_info.php" rel="external nofollow" >发布信息</a> <a href="tuichuchuli.php" rel="external nofollow" >退出系统</a> </p> <table class="table table-bordered mess" > <caption > 留言信息: </caption> <thead> <tr> <th>发送人</th> <th>发送时间</th> <th>接收人</th> <th>信息内容</th> </tr> </thead> <tbody> <?php require_once "./DBDA.class.php"; $db = new DBDA(); $sql = "select * from liuyan where recever='{$uid}' or recever='all'"; $arr = $db->query($sql,0); foreach($arr as $v){ echo "<tr> <td>{$v[1]}</td> <td>{$v[2]}</td> <td>{$v[3]}</td> <td>{$v[4]}</td> </tr>"; } ?> </tbody> </table> </body> </html>Exit the login system to implement user logout and return to the login page. The function code is as follows:
<?php session_start(); $uid = $_SESSION["uid"]; unset($uid); header("location:messlogin.php"); ?>The code is written here, and the more important part is completed. Below It is time to enter the release information page, which is equivalent to the added page written before. The processing page is no different from before. The difference is that the current processing page is operated when the user is logged in, and all logins need to be logged in using session. The pages under the circumstances are connected
The effect of the main page is as shown in the figure:
4. The last is the information release page, where you can send information to anyone
The code is as follows:##
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>发布信息界面</title> <script src="bootstrap/js/jquery-1.11.2.min.js"></script> <script src="bootstrap/js/bootstrap.min.js"></script> <link href="bootstrap/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/> </head> <style> .mess{ max-width: 200px; margin-top: 10px; } .mess1{ margin-top: 10px; } .opt{ max-width: 200px; margin-left: 80px; } .txt{ max-width: 200px; } </style> <body> <?php session_start(); $uid = $_SESSION["uid"]; if (empty($_SESSION["uid"])) { header("location:messlogin.php"); exit ; } ?> <p > <p > <a href="message.php" rel="external nofollow" >查看信息</a> <a href="seemess.php" rel="external nofollow" >查看发送信息</a> </p> <form class="form-horizontal" role="form" action="infochuli.php" method="post"> <p class="form-group"> <label for="firstname" class="col-sm-2 control-label mess1">接收人:</label> <p class="form-group "> <select class="form-control opt" name="recever"> <option value="all">所有人</option> <?php require_once "./DBDA.class.php"; $db = new DBDA(); //这里可以给特定的朋友发送信息的sql语句 //$sql = "select firend.firend,yuangong.name from firend,yuangong where firend.firend //= yuangong.username and firend.me = '{$uid}'"; $sname = "select * from yuangong where username not in ('{$uid}')"; $arr = $db->query($sname,0); //var_dump($arr[0][2]); foreach($arr as $v){ echo "<option value='{$v[0]}'>{$v[2]}</option>"; } ?> </select> </p> </p> <p class="form-group"> <label for="lastname" class="col-sm-2 control-label mess1">信息内容:</label> <p class="col-sm-10"> <textarea class="form-control txt" rows="3" name="content"></textarea> </p> </p> <p class="form-group"> <p class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default"> 发送 </button> </p> </p> </form> </p> </body> </html>
Send The information page is as shown in the figure:
5. After publishing the information, you have to enter the processing page, which is the submitted infochuli.php, and finally return to send Information interface
<?php session_start(); $uid = $_SESSION["uid"]; $recever = $_POST["recever"]; $content = $_POST["content"]; $arr = $_POST["recever"]; $t = date("Y-m-d H:i:s"); require_once "./DBDA.class.php"; $db = new DBDA(); $sql = "insert into liuyan values('','{$uid}','{$t}','{$recever}','{$content}',0)"; $arr = $db->query($sql); if($arr && !empty($arr)){ header("location:publish_info.php"); }else{ echo "发送失败!"; } ?>
The above is the detailed content of Use PHP to connect to the database to implement the message board function. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools