溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么使用PHP連接數(shù)據(jù)庫實現(xiàn)留言板功能

發(fā)布時間:2021-07-08 09:44:27 來源:億速云 閱讀:172 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下怎么使用PHP連接數(shù)據(jù)庫實現(xiàn)留言板功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

PHP實現(xiàn)留言板功能:

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">
  <div class="header">
   <h3>開發(fā)部內(nèi)部留言板</h3>
   <div class="input-group xiugai">
    <span class="input-group-addon" >用戶名:</span>
    <input type="text" class="form-control" name="uid" placeholder="請輸入用戶名">
   </div>
   <div class="input-group xiugai" >
    <span class="input-group-addon">口令:</span>
    <input type="text" class="form-control" name="pwd" placeholder="請輸入口令">
   </div>
   <button type="submit" class="btn btn-success login">登錄</button>
  </div>
 </form>
 </body>
</html>

2 登錄頁面完成后要進(jìn)入登錄處理頁面了,也就是上面提交到的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");
}

?>

登錄頁面效果如圖:

怎么使用PHP連接數(shù)據(jù)庫實現(xiàn)留言板功能

3.登錄完成后是進(jìn)入主頁面,也就是顯示自己收到的對話內(nèi)容,下面是設(shè)計的數(shù)據(jù)庫的表格和主頁面的代碼:

怎么使用PHP連接數(shù)據(jù)庫實現(xiàn)留言板功能

<!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;
  }
  ?>
  <div >
   <a href="publish_info.php" rel="external nofollow" >發(fā)布信息</a>
   <a href="tuichuchuli.php" rel="external nofollow" >退出系統(tǒng)</a>
   </div>
  <table class="table table-bordered mess" >
   <caption >
    留言信息:
   </caption>
   
   <thead>
    <tr>
     <th>發(fā)送人</th>
     <th>發(fā)送時間</th>
     <th>接收人</th>
     <th>信息內(nèi)容</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>

退出登錄系統(tǒng)實現(xiàn)用戶注銷,返回登錄頁面功能代碼如下:

 <?php
session_start();
$uid = $_SESSION["uid"];
unset($uid);
header("location:messlogin.php");

?>

代碼寫到這里,比較重要的部分就完成了,下面是要進(jìn)入發(fā)布信息頁面了,相當(dāng)于之前寫的添加的頁面,其處理頁面也是和之前沒什么區(qū)別的,差別在于現(xiàn)在的處理頁面是在用戶登錄的情況下操作的,需要用session把所有的登錄情況下的頁面連接起來

主頁面效果如圖:

怎么使用PHP連接數(shù)據(jù)庫實現(xiàn)留言板功能

怎么使用PHP連接數(shù)據(jù)庫實現(xiàn)留言板功能

4.最后是信息發(fā)布頁面,可以給任何人發(fā)送信息

代碼如下:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>發(fā)布信息界面</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 ;
}
?>
 <div >
  <div >
   <a href="message.php" rel="external nofollow" >查看信息</a>
   <a href="seemess.php" rel="external nofollow" >查看發(fā)送信息</a>
   </div>
  <form class="form-horizontal" role="form" action="infochuli.php" method="post">
   
   <div class="form-group">
     <label for="firstname" class="col-sm-2 control-label mess1">接收人:</label>
     <div class="form-group ">
      <select class="form-control opt" name="recever">
       <option value="all">所有人</option>
      <?php
      
      require_once "./DBDA.class.php";
      $db = new DBDA(); 

       //這里可以給特定的朋友發(fā)送信息的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>
     </div>
    </div>
   
   <div class="form-group">
    <label for="lastname" class="col-sm-2 control-label mess1">信息內(nèi)容:</label>
    <div class="col-sm-10">
     <textarea class="form-control txt" rows="3" name="content"></textarea>
    </div>
   </div>
   <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
     <button type="submit" class="btn btn-default">
     發(fā)送
     </button>
    </div>
   </div>
  </form>
 </div>

 </body>
</html>

發(fā)信息頁面如圖:

怎么使用PHP連接數(shù)據(jù)庫實現(xiàn)留言板功能

5.發(fā)布信息完成后要進(jìn)入處理頁面了,也就是提交到的infochuli.php,最后返回發(fā)送信息界面

<?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 "發(fā)送失敗!";
}

?>

以上是“怎么使用PHP連接數(shù)據(jù)庫實現(xiàn)留言板功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI