thinkphp入门学习,一个简单的用户管理系统
1.数据表
CREATE TABLE [dbo].[FXUSER](
[USER_ID] [int] NOT NULL,
[USER_NAME] [nvarchar](20) NOT NULL,
[USER_PASSWORD] [nvarchar](20) NOT NULL,
[USER_SEX] [int] NOT NULL,
[USER_HOBBY] [nvarchar](20) NOT NULL,
[USER_TYPE] [nvarchar](10) NOT NULL,
CONSTRAINT [PK_FXUSER] PRIMARY KEY CLUSTERED
2.项目结构
其中和MVC模型对应的控制器在Contoller下面,视图在View下面,模型层在Model下面
3.各文件代码
config.php
<?php
return array(
//'配置项'=>'配置值'
/*数据库设置*/
'DB_TYPE' => 'sqlsrv', // 数据库类型
'DB_HOST' => 'localhost,1433', // 服务器地址
'DB_NAME' => 'fxcl', // 数据库名
'DB_USER' => 'fxcl', // 用户名
'DB_PWD' => 'fxcl', // 密码
'DB_PORT' => '', // 端口
'DB_CHARSET' => 'utf8', // 数据库编码默认采用utf8
);
IndexController.class.php
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){
$this->display("index");
}
public function login(){
$username = $_REQUEST["username"];
$password = $_REQUEST["password"];
$user = M("fxuser")->where("USER_NAME='%s' AND USER_PASSWORD='%s' ",$username,$password)->find();
if($user==null){
$this->error("用户名或密码不正确","index");
}else{
if($user!=false){
$this->success("登陆成功!", "query");
}else{
$this->redirect("error", null);
}
}
}
public function query(){
$userlist = M("fxuser")->select();
$this->assign("userlist",$userlist);
$this->display("query");
}
public function delete(){
$userid = $_GET["id"];
$user = M("fxuser")->where("USER_ID=%d",$userid)->delete();
if($user!=false){
$this->success("删除成功!", "../../query");
}else{
$this->redirect("error", null);
}
}
public function update(){
$userid = $_GET["id"];
$user = M("fxuser")->where("USER_ID=%d",$userid)->find();
if($user==null){
$this->redirect("error", null);
}else{
if($user!=false){
$this->assign("user",$user);
$this->display("update");
}else{
$this->redirect("error", null);
}
}
}
public function userupdate(){
$user = M("fxuser");
$user_id = $_REQUEST["user_id"];
$user->USER_NAME = $_REQUEST["user_name"];
$user->USER_PASSWORD = $_REQUEST["user_password"];
$user->USER_SEX = $_REQUEST["user_sex"];
$user->USER_HOBBY = $_REQUEST["user_hobby"];
$user->USER_TYPE = $_REQUEST["user_type"];
$result = $user->where("USER_ID=%d",$user_id)->save();
if($result!=false){
$this->success("更新成功!", "query");
}else{
$this->redirect("error", null);
}
}
public function usersave(){
$user = M("fxuser");
$user->USER_ID = $_REQUEST["user_id"];
$user->USER_NAME = $_REQUEST["user_name"];
$user->USER_PASSWORD = $_REQUEST["user_password"];
$user->USER_SEX = $_REQUEST["user_sex"];
$user->USER_HOBBY = $_REQUEST["user_hobby"];
$user->USER_TYPE = $_REQUEST["user_type"];
$result =$user->add();
if($result!=false){
$this->success("添加成功!", "query");
}else{
$this->redirect("error", null);
}
}
public function save(){
$this->display("save");
}
}
error.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>错误</title>
</head>
<body>
发生了错误,请联系系统管理员
</body>
</html>
Index.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登陆</title>
</head>
<body>
<center>
<table width="60%" align="center">
<form action="login" method="post" >
<tr>
<td colspan="2" align="center">
<h1>用户登陆</h1>
</td>
</tr>
<tr></tr>
<tr>
<td width="45%" align="right">用户名:</td>
<td width="55%" align="left"><input name ="username" type="text" /></td>
</tr>
<tr>
<td align="right">密码:</td>
<td align="left"><input name ="password" type="text" /></td>
</tr>
<tr></tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="登陆" />
</td>
</tr>
</form>
</table>
</center>
</body>
</html>
query.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>显示用户信息</title>
</head>
<body>
<center>
<h1>用户信息</h1>
<table border="1" width="60%">
<tr>
<th>用户ID</th>
<th>用户名</th>
<th>密码</th>
<th>性别</th>
<th>爱好</th>
<th>类型</th>
<th>删除操作</th>
<th>更新操作</th>
</tr>
<foreach name="userlist" item="user" >
<tr>
<td align="left">{$user.user_id}</td>
<td align="left">{$user.user_name}</td>
<td align="left">***</td>
<td><if condition="($user.user_sex eq 1)"> 男<else />女</if></td>
<td align="left">{$user.user_hobby}</td>
<td>{$user.user_type}</td>
<td><a href="delete/id/{$user.user_id}" >删除</a></td>
<td><a href="update/id/{$user.user_id}" >更新</a></td>
</tr>
</foreach>
</table>
<br>
<a href="save">添加用户</a>
</center>
</body>
</html>
save.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加用户信息</title>
</head>
<body>
<center>
<table width="60%" align="center">
<form action="usersave" method="POST" >
<tr>
<td colspan ="2" align="center">
<h1>添加用户信息</h1>
</td>
</tr>
<tr>
<td width="45%" align="right">用户ID:</td><td width="55%" align="left"><input type="text" name="user_id" /></td>
</tr>
<tr>
<td align="right" >用户名:</td><td align="left"><input type="text" name="user_name" /></td>
</tr>
<tr>
<td align="right" >密码:</td><td align="left"><input type="text" name="user_password" /></td>
</tr>
<tr>
<td align="right" >性别:</td><td align="left"><input type="radio" name="user_sex" value="1" />男 <input type="radio" name="user_sex" value="2" />女</td>
</tr>
<tr>
<td align="right" >爱好:</td><td align="left"><input type="text" name="user_hobby" /></td>
</tr>
<tr>
<td align="right" >类型:</td><td align="left"><input type="text" name="user_type" /></td>
</tr>
<tr></tr>
<tr>
<td colspan ="2" align="center"><input type="submit" value="添加" /> <input type="reset" value="重置" /></td>
</tr>
</form>
</table>
</center>
</body>
</html>
update.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加用户信息</title>
</head>
<body>
<center>
<table width="60%" align="center">
<form action="usersave" method="POST" >
<tr>
<td colspan ="2" align="center">
<h1>添加用户信息</h1>
</td>
</tr>
<tr>
<td width="45%" align="right">用户ID:</td><td width="55%" align="left"><input type="text" name="user_id" /></td>
</tr>
<tr>
<td align="right" >用户名:</td><td align="left"><input type="text" name="user_name" /></td>
</tr>
<tr>
<td align="right" >密码:</td><td align="left"><input type="text" name="user_password" /></td>
</tr>
<tr>
<td align="right" >性别:</td><td align="left"><input type="radio" name="user_sex" value="1" />男 <input type="radio" name="user_sex" value="2" />女</td>
</tr>
<tr>
<td align="right" >爱好:</td><td align="left"><input type="text" name="user_hobby" /></td>
</tr>
<tr>
<td align="right" >类型:</td><td align="left"><input type="text" name="user_type" /></td>
</tr>
<tr></tr>
<tr>
<td colspan ="2" align="center"><input type="submit" value="添加" /> <input type="reset" value="重置" /></td>
</tr>
</form>
</table>
</center>
</body>
</html>
5.效果图