项目需求
1.用户注册、登陆
2.发表留言
3.删除本人留言
4.查看本人留言
项目环境参数
ThinkPHP3.2.3
PhpStorm
项目结构
message_board.pngMysql数据库建表
注意 sql语句中 `` 与 '' 的区别
创建用户表
DROP TABLE
IF EXISTS `jiat_user`;
CREATE TABLE `jiat_user` (
`user_id` INT (10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`username` VARCHAR (40) NOT NULL,
`password` CHAR (32) NOT NULL,
`create_at` INT (10) NOT NULL,
PRIMARY KEY (`user_id`),
KEY `createAt` (`create_at`) USING BTREE,
KEY `username` (`username`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
创建留言表
DROP TABLE
IF EXISTS `jiat_message`;
CREATE TABLE `jiat_message` (
`message_id` INT (10) UNSIGNED NOT NULL AUTO_INCREMENT,
`content` VARCHAR (100) NOT NULL,
`create_at` INT (10) NOT NULL,
`user_id` INT (10) UNSIGNED NOT NULL,
PRIMARY KEY (`message_id`),
KEY `createAt` (`create_at`) USING BTREE,
KEY `userId` (`user_id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
ThinkPHP中的数据库配置
路径:message_borad/Application/Home/Conf/config.php
<?php
return array(
//'配置项'=>'配置值'
'DB_TYPE' => 'mysql',
'DB_HOST' => 'localhost',
'DB_PORT' => 3306,
'DB_USER' => 'root',
'DB_PWD' => '',
'DB_NAME' => 'message_board',
'DB_PREFIX' => 'jiat_'
);
Model层
创建MessageViewModel.class.php
路径:message_borad/Application/Home/Model/MessageViewModel.class.php
<?php
/**
* Created by PhpStorm.
* User: tao1024
* Date: 2018/3/9
* Time: 23:25
*/
namespace Home\Model;
use Think\Model\ViewModel;
class MessageViewModel extends ViewModel {
public $viewFields = array(
'Message' => array('message_id', 'content', 'create_at'),
'User' => array('user_id', 'username', '_on' => 'Message.user_id=User.user_id')
);
}
Controller层
1.创建UserController.class.php
路径:message_borad/Application/Home/Controller/UserController.class.php
<?php
/**
* Created by PhpStorm.
* User: tao1024
* Date: 2018/3/9
* Time: 23:35
* 用户控制器
*/
namespace Home\Controller;
use think\Controller;
use think\Error;
use Think\Model;
class UserController extends Controller {
//注册
function register() {
$this->display();
}
//注册处理
function do_register() {
$username = I('username');
$password = I('password');
$password_repeat = I('password_repeat');
if (empty($username)) {
$this->error('用户名不能为空');
}
if (empty($password)) {
$this->error('密码不能为空');
}
if ($password != $password_repeat) {
$this->error('两次输入的密码不一致');
}
$userModel = new Model('User');
$user = $userModel->where(array("username" => $username))->find();
if (!empty($user)) {
$this->error('用户名已存在');
}
$userInfo = array("username" => $username, "password" => md5($password), "create_at" => time());
if (!($userModel->create($userInfo) && $userModel->add())) {
$this->error('注册失败' . $userModel->getDbError);
}
$this->success('注册成功', U('login'));
}
//登陆
function login() {
$this->display();
}
//登陆处理
function do_login() {
$username = I('username');
$password = I('password');
if (empty($username)) {
$this->error('用户名不能为空');
}
if (empty($password)) {
$this->error('密码不能为空');
}
$userModel = new Model('User');
$user = $userModel->where(array("username" => $username))->find();
if (empty($user)) {
$this->error('用户不存在');
}
if ($user["password"] != md5($password)) {
$this->error('密码错误');
}
//写入session
session('user.user_id', $user['user_id']);
session('user.username', $user['username']);
//跳转首页
$this->redirect('Index/index');
}
//退出
function logout() {
if (!session('user.user_id')) {
$this->error('请登录');
}
session_destroy();
$this->success('退出成功', U('Index/index'));
}
}
2.创建IndexController.class.php
路径:message_borad/Application/Home/Controller/IndexController.class.php
<?php
/**
* Created by PhpStorm.
* User: tao1024
* Date: 2018/3/9
* Time: 23:35
* 留言控制器
*/
namespace Home\Controller;
use Home\Model\MessageViewModel;
use Think\Controller;
use Think\Model;
use Think\Page;
class IndexController extends Controller {
/**
* 留言列表
*/
public function index() {
$messageModel = new MessageViewModel();
$count = $messageModel->count();
$page = new Page($count, 10);
$show = $page->show();
$message_list = $messageModel->order("message_id desc")->limit($page->firstRow . "," . $page->listRows)->select();
dump($message_list);
die();
$this->assign("page", $show);
$this->assign("message_list", $message_list);
$this->display();
}
/**
* 发表留言
*/
public function add() {
//$this->check_login();
$this->display();
}
/**
* 处理发表留言
*/
public function do_add() {
$this->check_login();
$content = I("content");
if (empty($content)) {
$this->error("留言内容不能为空");
}
if (mb_strlen(trim($content), "utf-8") > 100) {
$this->error("留言内容不能超过100个字");
}
$messageModel = new Model("Message");
$user_id = session("user.user_id");
$data = array("content" => $content, "user_id" => $user_id, "create_at" => time());
if (!($messageModel->create($data) && $messageModel->add())) {
$this->error("发表留言失败");
}
$this->success("发表留言成功!", U("Index/index"));
}
/**
* 删除留言
*/
public function delete() {
$message_id = I("message_id");
if (empty($message_id)) {
$this->error("缺少参数");
}
$this->check_login();
$messageModel = new Model("Message");
$result = $messageModel->where(array("message_id" => $message_id, "user_id" => session('user.user_id')))->delete();
if (!$result) {
$this->error("删除失败");
}
$this->success("删除成功!", U("Index/index"));
}
/**
* 检测登陆
*/
public function check_login() {
if (!session("user.user_id")) {
$this->error("请先登陆", U("User/login"));
}
}
}
View层
1.创建register.html
路径:message_borad/Application/Home/View/User/register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<h1>用户注册</h1>
<form method="post" action="/index/do_register">
<table>
<tr>
<td><label for="username">账号</label></td>
<td><input type="text" name="username" id="username" required/></td>
</tr>
<tr>
<td><label for="password">密码</label></td>
<td><input type="password" name="password" id="password" required/></td>
</tr>
<tr>
<td><label for="password_repeat">再次输入密码</label></td>
<td><input type="password" name="password_repeat" id="password_repeat" required/></td>
</tr>
<tr>
<td colspan="2" align="center">
<button>注册</button>
<button type="reset">重置</button>
</td>
</tr>
</table>
</form>
<p>
<a href="/index/login">已有账号?点击登录</a>
</p>
</body>
</html>
<!--注意点:
/index:当前模块的url地址,不包含域名
:当前项目的url地址,不包含域名
-->
2.创建login.html
路径:message_borad/Application/Home/View/User/login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陆</title>
</head>
<body>
<h1>用户登陆</h1>
<form method="post" action="/index/do_login">
<table>
<tr>
<td><label for="username">账号</label></td>
<td><input type="text" name="username" id="username" required/></td>
</tr>
<tr>
<td><label for="password">密码</label></td>
<td><input type="password" name="password" id="password" required/></td>
</tr>
<tr>
<td colspan="2" align="center">
<button>登陆</button>
<button type="reset">重置</button>
</td>
</tr>
</table>
</form>
<p>
<a href="/index/register">没有有账号?点击注册</a>
</p>
</body>
</html>
3.创建add.html
路径:message_borad/Application/Home/View/Index/add.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>发表留言</title>
</head>
<body>
<form method="post" action="/index/do_add">
<label for="content">留言内容</label><br/>
<textarea type="text" name="content" id="content" maxlength="100" rows="4" required></textarea><br/>
<button>发表</button>
<button type="reset">重置</button>
</form>
<p><a href="/index/index">首页</a></p>
</body>
</html>
4.创建index.html
路径:message_borad/Application/Home/View/Index/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>留言板</title>
</head>
<body>
<h1>留言板</h1>
<div>
<empty name="Think.session.user.username">
<a href="{:U('User/login')}">登陆</a>
<a href="{:U('User/register')}">注册</a>
<else/>
<p>{$Think.session.user.username},欢迎您!<a href="{:U('Index/add')}">发表留言</a> <a href="{:U('User/logout')}">退出登录</a></p>
</empty>
<volist name="message_list" id="item">
<div>
{$item.content}<br/>
留言者:{$item.user_id}
留言时间:{$item.create_at|date='Y-m-d H:i:s',###}
<eq name="Think.session.user.user_id" value="$item.user_id">
<a href="{:U('delete?message_id='.$item['message_id'])}" onclick="return confirm('确定删除此条留言?')">删除</a>
</eq>
</div>
</volist>
</div>
</body>
</html>
<!--注意点:
{:U('')}的用法
{$item.content}与value="$item.content"的区别-->
项目测试
1.用户注册
http://localhost/message_board/index.php/Home/User/register
2.用户登录
http://localhost/message_board/index.php/Home/User/login
3.发表留言
http://localhost/message_board/index.php/Home/Index/add
4.留言列表
http://localhost/message_board/index.php/Home/Index/index
网友评论