PHP简易表单验证与简易留言板实例
来源:互联网 发布:oa管理系统数据库设计 编辑:程序博客网 时间:2024/06/01 22:14
PHP简易表单验证与简易留言板实例
大家好,我是Yangrl.
初学MySQL,结合所学PHP写了一个简易的表单填写不为空后连接数据库插入留言信息的留言板例子,为什么我每次都要带上简易俩字呢?因为我。。懒(懒即生产力)!网上看了很多PHP表单验证,一万张相同的脸。(那我也就不与众不同了,也是一样的。{$ps:我™人肯定要与众不同啊,代码一样还不是™因为懒})[狗头吐舌.png],有很多不足,希望大家能指出!那么来看看实例吧o( ̄︶ ̄)o
验证和插入留言信息部分:
<?phpheader('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运行出来的要帅些哈?哈哈哈哈~嗝!)
表单为空直接提交,被提示:
输入信息后成功提交:
Perfect!
“一个人快乐或悲伤,只要不是装出来的,就必有其道理。你可以去分享他的快乐,同情他的悲伤,却不可以命令他怎样怎样他,因为这是违背人类的天性的。”——王小波
阅读全文
0 0
- PHP简易表单验证与简易留言板实例
- PHP简易留言板
- PHP简易留言板
- 人人都能做PHP简易留言板
- 简易留言板
- 简易留言板
- php简易表单
- Servlet简易验证实例
- 简易PHP mvc实例
- Easy Validate简易表单验证
- 简易JQuery表单验证插件
- WebStorage简易Web留言板
- Vue制作简易留言板!
- vue简易留言板
- [React]简易留言板
- JavaScript练习四之简易表单验证
- Jsp简易留言板的实现:application与Arraylist应用示例
- php写一个简易的登录表单
- Vue学习之computed
- git
- FastqC结果简介
- 2017/12/20 随笔记录
- 淘淘商城项目 第一天总结
- PHP简易表单验证与简易留言板实例
- 测试用例设计方法
- [简单逻辑学]逻辑学的基本原理——比较
- 吴恩达 神经网络和深度学习 第一部分课程 第四章课后习题 Building your Deep Neural Network
- 深度克隆
- 阿里HotFix热修复的简单使用
- SQLServer XML类型
- 【模板】高斯消元
- wangEditor的使用及上传图片(一)