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!

“一个人快乐或悲伤,只要不是装出来的,就必有其道理。你可以去分享他的快乐,同情他的悲伤,却不可以命令他怎样怎样他,因为这是违背人类的天性的。”——王小波

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值