溫馨提示×

溫馨提示×

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

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

php制作留言板功能

發(fā)布時間:2020-06-11 13:28:32 來源:PHP中文網 閱讀:831 作者:元一 欄目:編程語言

PHP簡介

PHP即“超文本預處理器”,是一種通用開源腳本語言。PHP是在服務器端執(zhí)行的腳本語言,與C語言類似,是常用的網站編程語言。根據動態(tài)網站要求,PHP語言作為一種語言程序,其專用性逐漸在應用過程中顯現(xiàn),其技術水平的優(yōu)劣與否將直接影響網站的運行效率。其特點是具有公開的源代碼, 在程序設計上與通用型語言,如C語言相似性較高,因此在操作過程中簡單易懂,可操作性強。

php實現(xiàn)簡單的留言板功能

1、原理

簡單的說就是 數(shù)據庫的創(chuàng)建,添加數(shù)據,顯示在前端上。我的程序只是簡單的留言再顯示。

首先寫好留言的前端頁面,就簡單的寫入作者,標題和內容。

2、界面

php制作留言板功能

3、顯示留言的界面

php制作留言板功能

4、代碼

(1)添加留言的頁面

<!DOCTYPE HTML>
    <HTML>
<Head>
    <meta  http-equiv="CONTENT-TYPE" ; content="text/html"  ; charset="UTF-8">
    <title>留言</title>
    <style type="text/css">
     .message{
         margin-top:0px;
     }
     h2{
         margin-top:200px;
     }
    </style>
</Head>
<Body>
   <h2 align="center">留言板</h2>
   <div class="message">
       <form name="addform" id="addform" method="post" action="message_handle.php">
           <table type="text" align="center" border="1px,solid">
               <input type="hidden" id="id" name="id" />
            <tr>
               <td>標題</td>
               <td><input type="text" name="title" id="title"/></td>
            </tr>
            <tr>
                <td>作者</td>
                <td><input type="text" name="author" id="author"/> </td>
            </tr>
            <tr>
                <td>內容</td>
                <td><textarea name="message" id="message" cols="60" role="15"></textarea></td>
            </tr>
            <tr>
                <td><input type="submit" name="sumbit"/></td>
                <td><input type="reset" name="reset"/></td>
            </tr>
           </table>
       </form>
   </div>
</Body>
</HTML>

(2)留言的后臺處理,把作者,標題,內容存入建好的數(shù)據庫中

<?php
header("CONTENT-TYPE:text/html;charset=UTF-8");
define("HOST","127.0.0.1");
define("USERNAME","root");
define("PASSWORD","");
if($con=new mysqli(HOST,USERNAME,PASSWORD,"test")){
    echo $con->error;
}
if($con->select_db("messageboard")){
    echo $con->error;
}
if($con->query("SET NAMES utf8")){
    echo $con->error;
}
$id=$_POST["id"];
$title=$_POST["title"];
$author=$_POST["author"];
$message=$_POST["message"];
$time=date('y-m-d h:m:s');
$sql="insert into messageboard(id,title,author,message,dateline) values('$id','$title','$author','$message','$time')";
if($str=$con->query($sql)){
    echo "<script>alert('留言成功');window.location.href='show_message.php'</script>";
}
else {
    echo "<script>alert('留言失敗');window.location.href='messageboard.php'</script>";
}
?>

(3)下面是顯示留言的頁面代碼

<?php
header("CONTENT-TYPE:text/html;charset=UTF-8");
define("HOST","127.0.0.1");
define("USERNAME","root");
define("PASSWORD","");
if($con=new mysqli(HOST,USERNAME,PASSWORD,"test")){
    echo $con->error;
}
if($con->select_db("messageboard")){
    echo $con->error;
}
if($con->query("SET NAMES utf8")){
    echo $con->error;
}
$sql="select * from messageboard ORDER BY dateline DESC ";
$str=$con->query($sql);
if($str && mysqli_num_rows($str)){
    while($row= mysqli_fetch_assoc($str)){
        $data[]=$row;
    }
}
?>
<!DOCTYPE HTML>
<HTML>
<Head>
    <meta  http-equiv="CONTENT-TYPE" ; content="text/html"  ; charset="UTF-8">
    <title>留言板</title>
    <style type="text/css">
    </style>
</Head>
<Body>
<div>
    <?php
    if(empty($data)){
        echo "當前沒有留言";
    }
    else{
    foreach($data as $value) {
    ?>
    <table cellpadding="2" cellspacing="8" align="center" border="1px,solid">
        <tr>
            <td>標題</td>
            <td><?php echo $value['title']; ?></td>
        </tr>
        <tr>
            <td>作者</td>
            <td><?php echo $value['author']; ?></td>
        </tr>
        <tr>
            <td>內容</td>
            <td><?php echo $value['message']; ?></td>
        </tr>
        <tr>
            <td><?php echo $value['dateline'];;?></td>
        </tr>
    </table>
</div>
<?php
 }
}
?>
</Body>
</HTML>

5、所遇到的問題

剛開始顯示頁面上不能顯示數(shù)據,找了半天原因,結果是因為在sql中寫錯了查詢方式寫成了:

select * from message where dateline desc;

用where得有條件,才能查詢到。得有例如:

select * from message where dateline=$date;

因為我的程序沒有從前個頁面?zhèn)鬟f數(shù)據到這,所以只能用下面這種通過時間來排序羅列出所有數(shù)據。

select * from message order by dateline;

以上就是php實現(xiàn)簡單的留言板功能(附源碼)的詳細內容,更多請關注億速云其它相關文章!

向AI問一下細節(jié)

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

AI