下面小編就為大家帶來一篇使用PHP連線資料庫實作留言板功能的實例講解(推薦)。小編覺得蠻不錯的,現在就分享給大家,也給大家做個參考。一起跟著小編過來看看吧
PHP實作留言板功能:
1 首先是登入頁面:
<!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 登入頁面完成後要進入登入處理頁面了,也就是上面提交到的messloginchuli.php
<?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"); } ?>
登入頁面效果如圖:
#3.登入完成後是進入首頁面,也就是顯示自己收到的對話內容,以下是設計的資料庫的表格和主頁面的程式碼:
<!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>
退出登入系統實現使用者登出,返回登入頁面功能代碼如下:
<?php session_start(); $uid = $_SESSION["uid"]; unset($uid); header("location:messlogin.php"); ?>
程式碼寫到這裡,比較重要的部分就完成了,在下面是要進入發布資訊頁面了,相當於之前寫的添加的頁面,其處理頁面也是和之前沒什麼區別的,差別在於現在的處理頁面是在用戶登錄的情況下操作的,需要用session把所有的登錄情況下的頁面連接起來
主頁面效果如圖:
#4.最後是訊息發布頁面,可以傳送訊息
##代碼如下:
<!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>###發資訊頁面如圖:#####################5.發布資訊完成後要進入處理頁面了,也就是提交到的infochuli.php,最後回傳資訊介面##################
<?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 "发送失败!"; } ?>
以上是php實作留言板功能(連接資料庫)的實例講解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

在PHP中,trait適用於需要方法復用但不適合使用繼承的情況。 1)trait允許在類中復用方法,避免多重繼承複雜性。 2)使用trait時需注意方法衝突,可通過insteadof和as關鍵字解決。 3)應避免過度使用trait,保持其單一職責,以優化性能和提高代碼可維護性。

依賴注入容器(DIC)是一種管理和提供對象依賴關係的工具,用於PHP項目中。 DIC的主要好處包括:1.解耦,使組件獨立,代碼易維護和測試;2.靈活性,易替換或修改依賴關係;3.可測試性,方便注入mock對象進行單元測試。

SplFixedArray在PHP中是一種固定大小的數組,適用於需要高性能和低內存使用量的場景。 1)它在創建時需指定大小,避免動態調整帶來的開銷。 2)基於C語言數組,直接操作內存,訪問速度快。 3)適合大規模數據處理和內存敏感環境,但需謹慎使用,因其大小固定。

PHP通過$\_FILES變量處理文件上傳,確保安全性的方法包括:1.檢查上傳錯誤,2.驗證文件類型和大小,3.防止文件覆蓋,4.移動文件到永久存儲位置。

JavaScript中處理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。 1.??返回第一個非null或非undefined的操作數。 2.??=將變量賦值為右操作數的值,但前提是該變量為null或undefined。這些操作符簡化了代碼邏輯,提高了可讀性和性能。

CSP重要因為它能防範XSS攻擊和限制資源加載,提升網站安全性。 1.CSP是HTTP響應頭的一部分,通過嚴格策略限制惡意行為。 2.基本用法是只允許從同源加載資源。 3.高級用法可設置更細粒度的策略,如允許特定域名加載腳本和样式。 4.使用Content-Security-Policy-Report-Only頭部可調試和優化CSP策略。

HTTP請求方法包括GET、POST、PUT和DELETE,分別用於獲取、提交、更新和刪除資源。 1.GET方法用於獲取資源,適用於讀取操作。 2.POST方法用於提交數據,常用於創建新資源。 3.PUT方法用於更新資源,適用於完整更新。 4.DELETE方法用於刪除資源,適用於刪除操作。

HTTPS是一種在HTTP基礎上增加安全層的協議,主要通過加密數據保護用戶隱私和數據安全。其工作原理包括TLS握手、證書驗證和加密通信。實現HTTPS時需注意證書管理、性能影響和混合內容問題。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 Linux新版
SublimeText3 Linux最新版

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版
中文版,非常好用

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。