Home > Article > Backend Development > An example of data update using thinkPHP [original], an example of thinkphp_PHP tutorial
In the previous article we implemented data deletion and batch deletion, in this article we will implement it Data updates.
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><</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>
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>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>
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.