在校学习ThinkPHP5的第一次上机考试,增删改查,非常简单。
数据表 user 字段值 id name password。TP5数据库的其它配置就不多说了。
首先在application\index\controller目录下建立控制器,命名为:Index.php?
Index.php 代码如下,
<?php
namespace app\index\controller;
use think\Controller;
use think\request;
use think\Db;
class Index extends Controller{
public function index()
{
return view('index');
}
//添加
public function add(){
$request = Request::instance();
$data = $request->post();
$res = Db::table('user')->insert($data);
if ($res) {
$this->success('添加成功', 'show');
} else {
$this->error('添加失败');
}
}
//显示
public function show()
{
$data=DB::table('user')->select();
//var_dump($data);
return view('show',['data' => $data]);
}
//删除
public function delete()
{
$request = Request::instance();
$id = $request->get('id');
$res = DB::table('user')->where('id='.$id)->delete();
if ($res) {
$this->success('删除成功', 'show');
} else {
$this->error('删除失败');
}
}
//查询
public function search(){
$keyword = input('keyword');
// var_dump($keyword);
$data = Db::table('user')->where('name','like',"%{$keyword}%")->select();//模糊查询
// var_dump($data);
return view('show',['data' => $data]);
}
//显示要修改的数据
public function update()
{
$request = Request::instance();
$id = $request->get('id');
$upl = Db::table('user')->where('id='.$id)->find();
return view('update',['upl' =>$upl]);
}
//修改数据
public function update_do()
{
$id = $_POST['id'];
$request = Request::instance();
$data = $request->post();
// var_dump($data);
$upl = Db::table('user')->where('id='.$id)->update($data);
if ($upl) {
$this->success('修改成功', 'show');
} else {
$this->error('修改失败');
}
}
}
?>
接着在application\index\view\index?目录下创建index.html,show.html,update.html页面
index.html代码如下,
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加</title>
</head>
<body>
<center>
<form action="add" method="post">
<table>
<tr>
<td>名称</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
show.html代码如下,
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>显示</title>
</head>
<body>
<center>
<form>
<input type="text" name="keyword" value="{$Request.param.keyword}">
<button type="submit">查询</button>
</form>
<br>
<br>
<table border=1>
<tr>
<th>id</th>
<th>name</th>
<th>password</th>
<th>相关操作</th>
</tr>
{foreach name="data" id="v"}
<tr>
<td>{$v.id}</td>
<td>{$v.name}</td>
<td>{$v.password}</td>
<td><a href="update?id={$v.id}">修改</a>
<a href="delete?id={$v.id}">删除</a>
</td>
</tr>
{/foreach}
</table>
</center>
</body>
</html>
update.html代码如下,
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改</title>
</head>
<body>
<center>
<table>
<form action="update_do" method="post">
<input type="hidden" name="id" value="{$upl.id}">
<tr>
<td>名称</td>
<td><input type="text" name="name" value="{$upl.name}"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="password" value="{$upl.password}"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
<td></td>
</tr>
</form>
</table>
</center>
</body>
</html>
|