本文实例讲述了tp5框架的增删改查操作。分享给大家供大家参考,具体如下:
controller控制器写入的内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
public function index()
{
$list = Db::table( 'user' )->select();
return view( 'login' ,[ 'list' => $list ]);
}
//增加页面
public function xian(){
return view( 'Xian' );
}
//增加至数据库
public function insert(){
$data = [
'id' =>null,
'name' => $_POST [ 'name' ],
'age' => $_POST [ 'age' ],
];
$list = Db::name( 'user' )->insert( $data );
if ( $list ){
echo "添加成功" ;
}
}
//删除数据库内容
public function del(){
$list = Db::table( 'user' )-> delete ( $_GET [ 'id' ]);
if ( $list ){
echo "删除成功" ;
}
}
//显示修改页面
public function gai(){
/* $list = $_GET;*/
return view( 'Gai' );
}
//修改数据库内容
public function upda(){
$list = Db::name( 'user' )->where( 'id' , $_POST [ 'id' ])->update([ 'name' => $_POST [ 'name' ], 'age' => $_POST [ 'age' ]]);
if ( $list ){
echo '修改成功' ;
}
}
|
这里是视图
修改的页面视图
1
2
3
4
5
6
7
|
<form action= "{:url('Login/upda')}" method= "post" >
name<input type= "text" name= "id" value= "{$_GET['id']}" >
name<input type= "text" name= "name" value= "{$_GET['name']}" ><br>
age<input type= "text" name= "age" value= "{$_GET['age']}" ><br>
<input type= "submit" >
</form>
{ $_GET [ 'id' ]} { $_GET [ 'name' ]} { $_GET [ 'age' ]}
|
这个代码就是用来取值的。
显示的页面视图
1
2
3
4
|
{ foreach $list as $v }
{ $v .name}--{ $v .age} <a href= "{:url('Login/del')}?id={$v.id}" rel= "external nofollow" rel= "external nofollow" >删除</a><a href= "{:url('Login/Gai')}?id={$v.id}&name={$v.name}&age={$v.age}" rel= "external nofollow" rel= "external nofollow" >修改</a><br>
{/ foreach }
<a href= "{:url('Login/xian')}" rel= "external nofollow" rel= "external nofollow" >添加</a>
|
显示的添加页面
1
2
3
4
|
{ foreach $list as $v }
{ $v .name}--{ $v .age} <a href= "{:url('Login/del')}?id={$v.id}" rel= "external nofollow" rel= "external nofollow" >删除</a><a href= "{:url('Login/Gai')}?id={$v.id}&name={$v.name}&age={$v.age}" rel= "external nofollow" rel= "external nofollow" >修改</a><br>
{/ foreach }
<a href= "{:url('Login/xian')}" rel= "external nofollow" rel= "external nofollow" >添加</a>
|
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。
原文链接:https://blog.csdn.net/weixin_43681591/article/details/86486756