在上一篇文章中我们实现了数据的删除和批量删除,这一篇文章我们将实现数据的更新。
首先依然是预期效果图:
点击修改后进入modi.html页面,然后进行修改,如此处修改了真实姓名这一属性:
点击保存:
以上是要实现的效果,下面是实现的具体代码了:
首先依然是视图内的代码:
<span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">role</span><span style="color: #0000ff;">="form"</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">="post"</span><span style="color: #ff0000;"> action</span><span style="color: #0000ff;">="__MODULE__/Admin/User/doAdd"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="input-group"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="input-group-addon"</span><span style="color: #0000ff;">></span>用<span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="__PUBLIC__/end/images/em.png"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">=""</span><span style="color: #ff0000;"> width</span><span style="color: #0000ff;">="6"</span><span style="color: #ff0000;"> height</span><span style="color: #0000ff;">="20"</span><span style="color: #0000ff;">></span>户<span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="__PUBLIC__/end/images/em.png"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">=""</span><span style="color: #ff0000;"> width</span><span style="color: #0000ff;">="6"</span><span style="color: #ff0000;"> height</span><span style="color: #0000ff;">="20"</span><span style="color: #0000ff;">></span>名:<span style="color: #0000ff;"></span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="form-control"</span><span style="color: #ff0000;"> placeholder</span><span style="color: #0000ff;">=""</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="username"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="input-group "</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="input-group-addon"</span><span style="color: #ff0000;"> for</span><span style="color: #0000ff;">="inputWarning1"</span><span style="color: #0000ff;">></span>真实姓名:<span style="color: #0000ff;"></span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="form-control"</span><span style="color: #ff0000;"> placeholder</span><span style="color: #0000ff;">=""</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="input"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="realname"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="input-group"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="input-group-addon"</span><span style="color: #0000ff;">></span>手机号码:<span style="color: #0000ff;"></span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="form-control"</span><span style="color: #ff0000;"> placeholder</span><span style="color: #0000ff;">=""</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="telphone"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="input-group"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="input-group-addon"</span><span style="color: #0000ff;">></span>电子邮箱:<span style="color: #0000ff;"></span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="form-control"</span><span style="color: #ff0000;"> placeholder</span><span style="color: #0000ff;">=""</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="email"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="input-group"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="input-group-addon"</span><span style="color: #0000ff;">></span>添加时间:<span style="color: #0000ff;"></span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="form-control"</span><span style="color: #ff0000;"> placeholder</span><span style="color: #0000ff;">="2014-05-22"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="resgistertime"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="input-group"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="input-group-addon"</span><span style="color: #0000ff;">></span>设置密码:<span style="color: #0000ff;"></span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="form-control"</span><span style="color: #ff0000;"> placeholder</span><span style="color: #0000ff;">="123456"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="password"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="input-group"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="input-group-addon"</span><span style="color: #0000ff;">></span>确认密码:<span style="color: #0000ff;"></span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="form-control"</span><span style="color: #ff0000;"> placeholder</span><span style="color: #0000ff;">="123456"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="repassword"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="input-group"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">button </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="btn btn-primary "</span><span style="color: #0000ff;">></span> <span style="color: #ff0000;"> </span>保<span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="__PUBLIC__/end/images/em.png"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">=""</span><span style="color: #ff0000;"> width</span><span style="color: #0000ff;">="20"</span><span style="color: #ff0000;"> height</span><span style="color: #0000ff;">="20"</span><span style="color: #0000ff;">></span>存<span style="color: #ff0000;"> </span><span style="color: #0000ff;"></span><span style="color: #800000;">button</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
接下来是控制器:需要说明的是这里需要对未修改前的值进行一下写入,主要是为了方便用户备忘,因此需要进行一个if分支的判断:
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> modi(){ </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (IS_POST) { </span><span style="color: #800080;">$adminUsersModel</span> = D("adminUsers"<span style="color: #000000;">); </span><span style="color: #800080;">$adminUsersModel</span>-><span style="color: #000000;">create(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> var_dump($adminUsersModel); // exit("创建成功!");</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$adminUsersModel</span>-><span style="color: #000000;">save()){ </span><span style="color: #800080;">$this</span>->success("修改成功", U("Admin/User/lists"<span style="color: #000000;">)); } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #800080;">$this</span>->error(<span style="color: #800080;">$adminUsersModel</span>-><span style="color: #000000;">getError()); } } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #800080;">$id</span> = <span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_GET</span>['id']) ? <span style="color: #008080;">intval</span>(<span style="color: #800080;">$_GET</span>['id']) : ''<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$id</span> == ''<span style="color: #000000;">) { </span><span style="color: #0000ff;">exit</span>("bad param! 请输入id"<span style="color: #000000;">); } </span><span style="color: #800080;">$adminUsersModel</span>=D("adminUsers"<span style="color: #000000;">); </span><span style="color: #800080;">$adminUsers</span>=<span style="color: #800080;">$adminUsersModel</span>->find(<span style="color: #800080;">$id</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">var_dump($adminUsers);</span> <span style="color: #800080;">$this</span>->assign("adminUsers", <span style="color: #800080;">$adminUsers</span><span style="color: #000000;">); </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">display(); } }</span>
本文为博主原创,转载请注明,并加原网址哦。
另外友情链接thinkPHP的参考手册:http://document.thinkphp.cn/manual_3_2.html
题外话:最近挺忙,除了查一些东西都没怎么跑到园子里来转一转,今天上线发现园子不如以前热闹了,我是有些小小的失落的,不知道各位园友是怎么看的,反正我觉得在这样一个挺嘈杂的社会环境下,能找到像园子这样的一个让人能静下心来写几行代码,思考生活,思考人生的地方真的不多了,还是希望大家能多多支持这样的一个平台,多多分享一些东西。作为准大三的我还希望能够得到各位大神的多多帮助啊。