Home >php教程 >php手册 >An example of data update using thinkPHP [Original]

An example of data update using thinkPHP [Original]

WBOY
WBOYOriginal
2016-07-06 13:28:22883browse

In the previous article we implemented data deletion and batch deletion. In this article we will implement data update.

First is still the expected rendering:

 

Click Modify to enter the modi.html page, and then make modifications, such as modifying the real name attribute here:

Click to save:

The above is the effect to be achieved. The following is the specific code to achieve:

First is still the code in the view:

<span style="color: #0000ff;"><</span><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><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><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;">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;">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><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><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><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><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><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><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><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><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><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><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><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><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><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><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><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><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><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><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><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><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><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><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>

Next is the controller: It should be noted that the unmodified value needs to be written here, mainly to facilitate the user's memo, so an if branch judgment needs to be made:

 

<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>

This article is original to the blogger. Please indicate the reprint and add the original URL.

Another friendly link to thinkPHP’s reference manual: http://document.thinkphp.cn/manual_3_2.html

Off-topic: I have been very busy recently. Apart from checking some things, I didn’t go to the garden much. I went online today and found that the garden is not as lively as before. I am a little disappointed. I don’t know what all garden friends are. How do you think about it? Anyway, I think in such a noisy social environment, there are really not many places like Yuanzi where people can calm down and write a few lines of code and think about life. I still hope. Can everyone support a platform like this more and share more things. As a quasi-junior, I also hope to get a lot of help from all the masters.

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