PHP簡易表單驗證與簡易留言板實例

PHP簡易表單驗證與簡易留言板實例

大家好,我是Yangrl.
初學MySQL,結合所學PHP寫了一個簡易的表單填寫不爲空後連接數據庫插入留言信息的留言板例子,爲什麼我每次都要帶上簡易倆字呢?因爲我。。懶(懶即生產力)!網上看了很多PHP表單驗證,一萬張相同的臉。(那我也就不與衆不同了,也是一樣的。{$ps:我™人肯定要與衆不同啊,代碼一樣還不是™因爲懶})[狗頭吐舌.png],有很多不足,希望大家能指出!那麼來看看實例吧o( ̄︶ ̄)o

驗證和插入留言信息部分:

<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
//初始化變量
$nickname=$title=$content="";
$nicknameError=$titleError=$contentError="";
function input_check($data) {
//去除字符(空格,tab,換行)
$data=trim($data);
//去除反斜槓\
$data=stripslashes($data);
//轉爲html實體
$data=htmlspecialchars($data);
return $data;
}
//如果訪問頁面使用的請求方法是POST,進行判斷
if($_SERVER["REQUEST_METHOD"]=="POST") {
    if(empty($_POST['username'])) {
        $nicknameError="快點編個假名字好絕人!";
    }else {
        $nickname=input_check($_POST['username']);
    }
    if(empty($_POST['title'])) {
        $titleError="留言還是要有個標題!";
    }else {
        $title=input_check($_POST['title']);
    }
    if(empty($_POST['content'])) {
        $contentError="莫把內容都搞忘了喲!";
    }else {
        $content=input_check($_POST['content']);
    }
    //如果都不爲空,就連接數據庫插入留言信息
    if(!empty($_POST['username']) && !empty($_POST['title']) && !empty($_POST['content'])) {
        if(isset($_POST['releaseMsg'])) {
        $user=trim(strip_tags($_POST['username']));
        $title=trim(strip_tags($_POST['title']));
        $content=trim(strip_tags($_POST['content']));
        $host="localhost";
        $username="root";
        $password="root";
        $dbname="ylc_mall";
        $conn=new mysqli($host,$username,$password,$dbname);
        if($conn->connect_error) {
            die("連接數據庫失敗!".$conn->connect_error);
        }
        $conn->set_charset("utf8");
        $sql="INSERT INTO usermesg(mes_username,mes_title,mes_content,mes_createtime) VALUES ('{$user}','{$title}','{$content}','{$_SERVER['REQUEST_TIME']}')";
        if($conn->query($sql)===TRUE) {
            echo "<script>alert('留言成功!');location.href='ylc_messageBoard.php';</script>";
        }else {
            echo "<script>alert('留言失敗!');location.href='ylc_messageBoard.php';</script>";
        }
        $conn->close();
        }
    }   
}
?>
<div class="mes_content">
    <table class="mes_table" cellspacing="0" cellspacing="0">
        <thead class="mes_thead">
            <tr>
                <th>
                    座位
                </th>
                <th>
                    暱稱
                </th>
                <th>
                    標題
                </th>
                <th>
                    內容
                </th>
                <th>
                    發佈時間
                </th>
            </tr>
        </thead>
        <tbody class="mes_tbody">
        <?php                               
        $host="localhost";
        $username="root";
        $password="root";
        $dbname="ylc_mall";
        $conn=new mysqli($host,$username,$password,$dbname);
        if($conn->connect_error) {
            die("連接數據庫失敗!".$conn->connect_error);
        }
        //查詢數據
        $sql="SELECT mes_id,mes_username,mes_title,mes_content,mes_createtime FROM usermesg ORDER BY mes_createtime DESC";
        $query=$conn->query($sql);
        if ($query->num_rows>=0) {
            while ($row=$query->fetch_assoc()) {
        ?>
            <tr class="success">
                <td>
                <?php echo $row['mes_id']; ?>
                </td>
                <td>
                <?php echo $row['mes_username']; ?>
                </td>
                <td class="mes_title_maxlen" title="<?php echo $row['mes_title']; ?>">
                <?php echo  $row['mes_title']; ?>
                </td>
                <td class="mes_content_maxlen" title="<?php echo $row['mes_content']; ?>">
                <?php echo $row['mes_content']; ?>
                </td>
                <td>
                <?php echo date("Y-m-d-H:i:s",$row['mes_createtime']); ?>
                </td>
            </tr>
            <?php 
                }
            }else {
                echo "無數據";
            }
            $conn->close();
            ?>
        </tbody>
    </table>
    <div class="mes_form">
        <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
            <h2>發佈</h2>
            <div class="control-group">
                <label class="control-label">暱稱</label>
                <div class="controls">
                    <input id="username" class="mes_inp" name="username" type="text" value="<?php echo $nickname; ?>" />
                    <span class="mes_error">*<?php echo $nicknameError; ?></span>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">標題</label>
                <div class="controls">
                    <input id="title" name="title"  class="mes_inp" type="title" value="<?php echo $title; ?>" />
                    <span class="mes_error">*<?php echo $titleError; ?></span>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">內容</label>
                <div class="controls">
                    <textarea name="content" id="content" cols="50" rows="5" ><?php echo $content; ?></textarea>
                    <span class="mes_error">*<?php echo $contentError; ?></span>
                </div>
            </div>
            <div>
                <input type="submit" name="releaseMsg" class="mes_btn" value="發佈" title="想好噢!">
            </div>
        </form>
    </div>
</div>

結果如下(樣式需要DIY喲~截圖是加了樣式之後的,怎麼樣?是比你們這些直接copy運行出來的要帥些哈?哈哈哈哈~嗝!)

表單爲空直接提交,被提示:
descirption

輸入信息後成功提交:
description

ojbk

Perfect!

“一個人快樂或悲傷,只要不是裝出來的,就必有其道理。你可以去分享他的快樂,同情他的悲傷,卻不可以命令他怎樣怎樣他,因爲這是違揹人類的天性的。”——王小波

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章