Home >php教程 >php手册 >使用thinkPHP实现数据更新一例【原创】,thinkphp一例

使用thinkPHP实现数据更新一例【原创】,thinkphp一例

WBOY
WBOYOriginal
2016-07-06 14:24:231008browse

使用thinkPHP实现数据更新一例【原创】,thinkphp一例

  在上一篇文章中我们实现了数据的删除和批量删除,这一篇文章我们将实现数据的更新。

  首先依然是预期效果图:

  

点击修改后进入modi.html页面,然后进行修改,如此处修改了真实姓名这一属性:

点击保存:

  以上是要实现的效果,下面是实现的具体代码了:

  首先依然是视图内的代码:

<span><</span><span>form </span><span>role</span><span>="form"</span><span> method</span><span>="post"</span><span> action</span><span>="__MODULE__/Admin/User/doAdd"</span><span>></span>
                  <span><</span><span>div </span><span>class</span><span>="input-group"</span><span>></span> <span><</span><span>span </span><span>class</span><span>="input-group-addon"</span><span>></span>用<span><</span><span>img </span><span>src</span><span>="__PUBLIC__/end/images/em.png"</span><span> alt</span><span>=""</span><span> width</span><span>="6"</span><span> height</span><span>="20"</span><span>></span>户<span><</span><span>img </span><span>src</span><span>="__PUBLIC__/end/images/em.png"</span><span> alt</span><span>=""</span><span> width</span><span>="6"</span><span> height</span><span>="20"</span><span>></span>名:<span></</span><span>span</span><span>></span>
                    <span><</span><span>input </span><span>type</span><span>="text"</span><span> class</span><span>="form-control"</span><span> placeholder</span><span>=""</span><span> name</span><span>="username"</span><span>></span>
                  <span></</span><span>div</span><span>></span>
                  <span><</span><span>div </span><span>class</span><span>="input-group "</span><span>></span> <span><</span><span>span </span><span>class</span><span>="input-group-addon"</span><span> for</span><span>="inputWarning1"</span><span>></span>真实姓名:<span></</span><span>span</span><span>></span>
                    <span><</span><span>input </span><span>type</span><span>="text"</span><span> class</span><span>="form-control"</span><span> placeholder</span><span>=""</span><span> id</span><span>="input"</span><span> name</span><span>="realname"</span><span>></span>
                  <span></</span><span>div</span><span>></span>
                  <span><</span><span>div </span><span>class</span><span>="input-group"</span><span>></span> <span><</span><span>span </span><span>class</span><span>="input-group-addon"</span><span>></span>手机号码:<span></</span><span>span</span><span>></span>
                    <span><</span><span>input </span><span>type</span><span>="text"</span><span> class</span><span>="form-control"</span><span> placeholder</span><span>=""</span><span> name</span><span>="telphone"</span><span>></span>
                  <span></</span><span>div</span><span>></span>
                  <span><</span><span>div </span><span>class</span><span>="input-group"</span><span>></span> <span><</span><span>span </span><span>class</span><span>="input-group-addon"</span><span>></span>电子邮箱:<span></</span><span>span</span><span>></span>
                    <span><</span><span>input </span><span>type</span><span>="text"</span><span> class</span><span>="form-control"</span><span> placeholder</span><span>=""</span><span> name</span><span>="email"</span><span>></span>
                  <span></</span><span>div</span><span>></span>
                  <span><</span><span>div </span><span>class</span><span>="input-group"</span><span>></span> <span><</span><span>span </span><span>class</span><span>="input-group-addon"</span><span>></span>添加时间:<span></</span><span>span</span><span>></span>
                    <span><</span><span>input </span><span>type</span><span>="text"</span><span> class</span><span>="form-control"</span><span> placeholder</span><span>="2014-05-22"</span><span> name</span><span>="resgistertime"</span><span>></span>
                  <span></</span><span>div</span><span>></span>
                <span><</span><span>div </span><span>class</span><span>="input-group"</span><span>></span> <span><</span><span>span </span><span>class</span><span>="input-group-addon"</span><span>></span>设置密码:<span></</span><span>span</span><span>></span>
                    <span><</span><span>input </span><span>type</span><span>="text"</span><span> class</span><span>="form-control"</span><span> placeholder</span><span>="123456"</span><span>  name</span><span>="password"</span><span>></span>
                  <span></</span><span>div</span><span>></span>
                <span><</span><span>div </span><span>class</span><span>="input-group"</span><span>></span> <span><</span><span>span </span><span>class</span><span>="input-group-addon"</span><span>></span>确认密码:<span></</span><span>span</span><span>></span>
                    <span><</span><span>input </span><span>type</span><span>="text"</span><span> class</span><span>="form-control"</span><span> placeholder</span><span>="123456"</span><span> name</span><span>="repassword"</span><span>></span>
                  <span></</span><span>div</span><span>></span>
                  <span><</span><span>div </span><span>class</span><span>="input-group"</span><span>></span>
                    <span><</span><span>button </span><span>type</span><span>="submit"</span><span> class</span><span>="btn btn-primary "</span><span>></span> <span>  </span>保<span><</span><span>img </span><span>src</span><span>="__PUBLIC__/end/images/em.png"</span><span> alt</span><span>=""</span><span> width</span><span>="20"</span><span> height</span><span>="20"</span><span>></span>存<span>  </span><span></</span><span>button</span><span>></span>
                  <span></</span><span>div</span><span>></span>
                <span></</span><span>form</span><span>></span>

  接下来是控制器:需要说明的是这里需要对未修改前的值进行一下写入,主要是为了方便用户备忘,因此需要进行一个if分支的判断:

  

<span>public</span> <span>function</span><span> modi(){
    </span><span>if</span><span> (IS_POST) {
            </span><span>$adminUsersModel</span> = D("adminUsers"<span>);
            </span><span>$adminUsersModel</span>-><span>create();
            </span><span>//</span><span> var_dump($adminUsersModel);
            // exit("创建成功!");</span>
            <span>if</span>(<span>$adminUsersModel</span>-><span>save()){                    
                </span><span>$this</span>->success("修改成功", U("Admin/User/lists"<span>));
            }
            </span><span>else</span><span> {
                </span><span>$this</span>->error(<span>$adminUsersModel</span>-><span>getError());
            }
    }

    </span><span>else</span><span> {
             </span><span>$id</span> = <span>isset</span>(<span>$_GET</span>['id']) ? <span>intval</span>(<span>$_GET</span>['id']) : ''<span>;
             </span><span>if</span> (<span>$id</span> == ''<span>) {
                </span><span>exit</span>("bad param! 请输入id"<span>);
             }
             </span><span>$adminUsersModel</span>=D("adminUsers"<span>);
             </span><span>$adminUsers</span>=<span>$adminUsersModel</span>->find(<span>$id</span><span>);
             </span><span>//</span><span>var_dump($adminUsers);</span>
             <span>$this</span>->assign("adminUsers", <span>$adminUsers</span><span>);
             </span><span>$this</span>-><span>display();
    }
}</span>

  本文为博主原创,转载请注明,并加原网址哦。

  另外友情链接thinkPHP的参考手册:http://document.thinkphp.cn/manual_3_2.html     

 

 

  题外话:最近挺忙,除了查一些东西都没怎么跑到园子里来转一转,今天上线发现园子不如以前热闹了,我是有些小小的失落的,不知道各位园友是怎么看的,反正我觉得在这样一个挺嘈杂的社会环境下,能找到像园子这样的一个让人能静下心来写几行代码,思考生活,思考人生的地方真的不多了,还是希望大家能多多支持这样的一个平台,多多分享一些东西。作为准大三的我还希望能够得到各位大神的多多帮助啊。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn