溫馨提示×

溫馨提示×

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

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

PHP留言板功能代碼分享

發(fā)布時間:2020-05-15 16:48:44 來源:億速云 閱讀:210 作者:Leah 欄目:編程語言

今天小編就為大家?guī)硪黄狿HP實現(xiàn)留言板功能的文章。小編覺得挺實用的,為此分享給大家做個參考。一起跟隨小編過來看看吧。

首先創(chuàng)建消息表,其主要字段有發(fā)送者的名稱,消息內(nèi)容,以及消息發(fā)送時間;

SQL:

CREATE TABLE `guanhui`.`message` (
  `id` INT(10) NOT NULL AUTO_INCREMENT COMMENT '消息ID' ,
  `sender` VARCHAR(60) NOT NULL COMMENT '發(fā)送者' ,
  `content` TEXT NOT NULL COMMENT '消息內(nèi)容' ,
  `send_time` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT '發(fā)送時間' ,
  PRIMARY KEY (`id`)
) ENGINE = MyISAM;

然后在前端創(chuàng)建表單并將留言消息查詢出來,進行列表展示;

表單HMTL:

<form action="./send_message.php" method="POST">


  <input type="text" name="sender" placeholder="你的昵稱">
  <textarea rows="4" name="content" placeholder="留言內(nèi)容"></textarea>
  <button type="submit">發(fā)送</button>
</form>

展示列表:

<?php
//鏈接數(shù)據(jù)庫
$conn = mysql_connect("loclhost:3306","root","root"); 
//判斷錯誤函數(shù)
if(!$conn){
   die(mysql_error());
}
//選擇數(shù)據(jù)庫
mysql_query("use message",$conn);
//設(shè)定字符集編碼
mysql_query("set names utf8",$conn);
//查詢語句
$sql = "select * from message";
//執(zhí)行語句
$res = mysql_query($sql);
//建立一個空數(shù)組
$data = array();
//執(zhí)行循環(huán)
while($row = mysql_fetch_assoc($res)){
     $data[] = $row;
}
?>
<table>
  <tr>
    <th>ID</th>
    <th>Name</th>
    <th>Sender</th>
    <th class="content">Content</th>
    <th>操作</th>
  </tr>
  <?php foreach($data as $k=>$v){?>  
    <tr>
      <td><?=$v['id']?></td>
      <td><?=$v['name']?></td>
      <td><?=$v['sender']?></td>
      <td><?=$v['content']?></td>
      <td>
      <a href="./update.php?id=<?=$v['id']?>">修改</a>
      <a href="./del.php?id=<?=$v['id']?>">刪除</a>
      </td>
    </tr>
  <?php }?>
<table>

最后將表單提交過來的信息保存到數(shù)據(jù)庫即可。

<?php
//鏈接數(shù)據(jù)庫
$conn = mysql_connect("loclhost:3306","root","root"); 
//判斷錯誤函數(shù)
if(!$conn){
   die(mysql_error());
}
//選擇數(shù)據(jù)庫
mysql_query("use message",$conn);
//設(shè)定字符集編碼
mysql_query("set names utf8",$conn);
 
//獲取表單值
$name = $_POST['name'];
$sender = $_POST['sender'];
$content =$_POST['content'];
//插入數(shù)據(jù)庫語句
$sql = "insert into message(name,sender,content)values('$name','$sender','$content')";
//執(zhí)行數(shù)據(jù)
$res = mysql_query($sql);
//判斷結(jié)果
if($res){
    echo "增加成功";
}else{
    die("增加失敗".mysql_error());
}

以上就是PHP實現(xiàn)留言板功能的詳細(xì)內(nèi)容,代碼示例簡單明了,如果在日常工作遇到此問題。通過這篇文章,希望你能有所收獲,更多詳情敬請關(guān)注億速云行業(yè)資訊頻道!

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

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

AI