PHP简易留言板

源码目录

效果图

 

 

使用jquery的扩展库实现了检测输入框值不能为空

index.php

 

<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 16-1-17
 * Time: 上午11:19
 */

echo '<center> 留言板 </center><a href="AddLiuyan.html">写留言</a><br><br>';


require 'ShowLiuyan.php';

ShowLiuyan.php

<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 16-1-17
 * Time: 上午11:36
 */
require 'DbMysqlConn.php';


$sql = 'select * from tb_liuyan';
$result = mysqli_query($conn,$sql);//查询结果返回成结果集

?>
<center>
<?php//遍历查询结果集
    while($row = mysqli_fetch_array($result)){
    echo $row['tb_liuyan_title'].'<br>'.$row['tb_liuyan_username'].'     '.$row['tb_liuyan_date'].'<br>'.$row['tb_liuyan_content'].'<br><br><hr>';
}?>
</center>
<?php//及时关闭数据库
require 'DbMysqlClose.php';
?>

DbMysqlConn.php

 

<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 16-1-17
 * Time: 上午11:29
 *
 * 由于增加留言和显示留言都需要链接数据库,所以单独抽出代码做成一个文件,方便调用和修改
 */
$ServerName = 'localhost';//mysql的服务器地址
$UserName = 'root';//mysql用户名
$UserPwd = '123456';//mysql密码
$DbName = 'liuyanban';//数据库名称

$conn = mysqli_connect($ServerName,$UserName,$UserPwd,$DbName);
//获取连接数据库

$conn->query('set names utf8') or die('query字符集错误');//这句一定不能忘记
//否则查询出来的中文会在页面显示问号

if(!$conn){
    die("Connection faild: ".mysqli_connect_error());
}



 

DbMysqlClose.php

 

<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 16-1-17
 * Time: 下午5:34
 */
mysqli_free_result($result);
mysqli_close($conn);


AddLiuyan.html

 

 

<html lang="zh">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>写留言</title>
    <script src="js/jquery.min.js"></script>
    <script src="js/jquery.validate.min.js"></script>
    <script type="text/javascript">
        $().ready(function () {
            $("#AddLiuyan").validate();
        });

    </script>
</head>
<body>
<br><br><br><br><br><br>
<center>
    <form id="AddLiuyan" method="POST" action="AddLiuyan.php">
        标题:<input type="text" name="title" id="title" class="required"><br><br>
        昵称:<input type="text" name="username" id="username" class="required"><br><br>
        内容:<textarea name="content" class="required"></textarea><br><br>
        <input type="submit" name="submit" value="留言" />
    </form>
</center>
</body>
</html>


AddLiuyan.php

 

 

<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 16-1-17
 * Time: 上午11:20
 */

include 'DbMysqlConn.php';

$lytime = date('Y-m-d h:i:s', time());//获取当前时间
if ($_POST[submit]) {
    $sql = "insert into tb_liuyan(tb_liuyan_username,tb_liuyan_title,tb_liuyan_content,tb_liuyan_date) values('$_POST[username]','$_POST[title]','$_POST[content]','{$lytime}')";
 mysqli_query($conn,$sql);//补充插入语句,并查询

//及时关闭数据库
include 'DbMysqlClose.php';
//插入完成后,跳回主界面
echo "<script type='text/javascript'> window.location.href = 'index.php' </script>";
}


?>

tb_liuyan.sql

 

-- phpMyAdmin SQL Dump
-- version 4.5.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: 2016-01-17 22:50:12
-- 服务器版本: 5.5.46-0+deb8u1
-- PHP Version: 5.6.14-0+deb8u1

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `liuyanban`
--

-- --------------------------------------------------------

--
-- 表的结构 `tb_liuyan`
--

CREATE TABLE `tb_liuyan` (
  `tb_liuyan_id` tinyint(1) NOT NULL,
  `tb_liuyan_username` varchar(25) NOT NULL,
  `tb_liuyan_title` varchar(50) NOT NULL,
  `tb_liuyan_content` text NOT NULL,
  `tb_liuyan_date` datetime NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- 转存表中的数据 `tb_liuyan`
--

INSERT INTO `tb_liuyan` (`tb_liuyan_id`, `tb_liuyan_username`, `tb_liuyan_title`, `tb_liuyan_content`, `tb_liuyan_date`) VALUES
(9, 'test', '我是测试书刊据', 'hushiukhkajshkjd阿拉善框架拉卡时间到了库阿拉开始觉得', '2016-01-17 10:46:17'),
(8, 'test', '这是测试数据', '简单留言板简单留言板简单留言板简单留言板简单留言板简单留言板简单留言板简单留言板简单留言板简单留言板简单留言板', '2016-01-17 10:45:52');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `tb_liuyan`
--
ALTER TABLE `tb_liuyan`
  ADD PRIMARY KEY (`tb_liuyan_id`),
  ADD UNIQUE KEY `tb_liuyan_id` (`tb_liuyan_id`);

--
-- 在导出的表使用AUTO_INCREMENT
--

--
-- 使用表AUTO_INCREMENT `tb_liuyan`
--
ALTER TABLE `tb_liuyan`
  MODIFY `tb_liuyan_id` tinyint(1) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

 

 

 

注意:

require与include的区别

文件、变量的命名规范

更多优质文章:正经的金莲

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值