在没介绍正文之前先给大家介绍下数据更新方法支持的连贯操作方法有:
在上一篇文章中我们实现了数据的删除和批量删除,这一篇文章我们将实现数据的更新。
首先依然是预期效果图:
点击修改后进入modi.html页面,然后进行修改,如此处修改了真实姓名这一属性:
点击保存:
以上是要实现的效果,下面是实现的具体代码了:
首先依然是视图内的代码:
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
|
<form role= "form" method= "post" action= "__module__/admin/user/doadd" > <div class = "input-group" > <span class = "input-group-addon" >用<img src= "__public__/end/images/em.png" alt= "" width= "6" height= "20" >户<img src= "__public__/end/images/em.png" alt= "" width= "6" height= "20" >名:</span> <input type= "text" class = "form-control" placeholder= "" name= "username" > </div> <div class = "input-group " > <span class = "input-group-addon" for = "inputwarning1" >真实姓名:</span> <input type= "text" class = "form-control" placeholder= "" id= "input" name= "realname" > </div> <div class = "input-group" > <span class = "input-group-addon" >手机号码:</span> <input type= "text" class = "form-control" placeholder= "" name= "telphone" > </div> <div class = "input-group" > <span class = "input-group-addon" >电子邮箱:</span> <input type= "text" class = "form-control" placeholder= "" name= "email" > </div> <div class = "input-group" > <span class = "input-group-addon" >添加时间:</span> <input type= "text" class = "form-control" placeholder= "2014-05-22" name= "resgistertime" > </div> <div class = "input-group" > <span class = "input-group-addon" >设置密码:</span> <input type= "text" class = "form-control" placeholder= "123456" name= "password" > </div> <div class = "input-group" > <span class = "input-group-addon" >确认密码:</span> <input type= "text" class = "form-control" placeholder= "123456" name= "repassword" > </div> <div class = "input-group" > <button type= "submit" class = "btn btn-primary " > 保<img src= "__public__/end/images/em.png" alt= "" width= "20" height= "20" >存 </button> </div> </form> |
接下来是控制器:需要说明的是这里需要对未修改前的值进行一下写入,主要是为了方便用户备忘,因此需要进行一个if分支的判断:
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
|
public function modi(){ if (is_post) { $adminusersmodel = d( "adminusers" ); $adminusersmodel ->create(); // var_dump($adminusersmodel); // exit("创建成功!"); if ( $adminusersmodel ->save()){ $this ->success( "修改成功" , u( "admin/user/lists" )); } else { $this ->error( $adminusersmodel ->geterror()); } } else { $id = isset( $_get [ 'id' ]) ? intval ( $_get [ 'id' ]) : '' ; if ( $id == '' ) { exit ( "bad param! 请输入id" ); } $adminusersmodel =d( "adminusers" ); $adminusers = $adminusersmodel ->find( $id ); //var_dump($adminusers); $this ->assign( "adminusers" , $adminusers ); $this ->display(); } } |
下面给大家介绍下thinkphp更新数据库的五种方法
第一种方法:
$模型->where(‘id=1')->save($data);
第二种方法:
$模型->where(‘id=1')->data($data)->save();
第三种方法:
$模型->create();
$模型->save();
表单中必须包含一个以主键为名称的隐藏域
第四种方法:
$模型->where('id=5')->setfield('name','thinkphp');
$模型->where('id=5')->setfield(array('name','email'),array('tp','tp@163.com'));
//第四种方法,传数组时候,我实现不了。。。
第五种方法:
$模型->setinc('score','id=5',3); // 积分加3
$模型->setinc('score','id=5'); // 积分加1
$模型->setdec('score','id=5',5); // 积分减5
$模型->setdec('score','id=5'); // 积分减1
原文链接:http://www.cnblogs.com/sunshine-s/archive/2016/06/29/5627148.html