Home > Backend Development > PHP Tutorial > ThinkPHP basic add, delete, check and modify operation example tutorial

ThinkPHP basic add, delete, check and modify operation example tutorial

不言
Release: 2023-03-30 06:58:02
Original
1467 people have browsed it

This article mainly introduces the basic add, delete, check and modify operations of ThinkPHP. It has a very wide range of applications in ThinkPHP programming. Friends in need can refer to it

The example of this article describes the basic add, delete, check and modify operations of ThinkPHP Operation is the most commonly used basic operation in ThinkPHP project development and has very important application value. Now I will share the complete example code with you, I hope it will be helpful to everyone. The details are as follows:

1. Table aoli_user field settings:

The table aoli_user mainly has the following fields:

id username password createtime createip

2. View template part

1. User home page template:

aoli/Home/Tpl/default/User/index The .html page code is as follows:

<form action="__URL__/add" method="post">
 用户名:<input type="text" name="username" /><br />
 密码:<input type="password" name="password" /><br />
 重复密码:<input type="repassword" name="repassword" /><br />
 <input type="submit" value="注册" />
</form>

<volist name="alist" id="vo">
 <li><span>ID:</span>{$vo[&#39;id&#39;]}<span>用户名:</span>{$vo[&#39;username&#39;]}<span>注册ip:</span>{$vo[&#39;createip&#39;]}<a href="__URL__/del/id/{$vo[&#39;id&#39;]}">删除</a>  <a href="__URL__/edit/id/{$vo[&#39;id&#39;]}">编辑</a></li>
</volist>
Copy after login

2. User edit template:

aoli/Home/Tpl/default The /User/edit.html page code is as follows:

<form action="__URL__/update" method="post">
 用户名:<input type="text" name="username" value="{$data[&#39;username&#39;]}" /><br />
 密码:<input type="password" name="password" value="{$data[&#39;password&#39;]}" /><br />
 IP:<input type="text" name="createip" value="{$data[&#39;createip&#39;]}" /><br />
 时间:<input type="text" name="createtime" value="{$data[&#39;createtime&#39;]}" /><br />
 <input type="hidden" value="{$data[&#39;id&#39;]}" name="id" />
 <input type="submit" value="更新" />
</form>
Copy after login

2. Action part:

aoli/Home/ The Lib/Action/UserAction.class.php page is as follows:

class UserAction extends Action {
   function index(){
     $user=M(&#39;user&#39;);
     $list=$user->field(array(&#39;id&#39;,&#39;username&#39;,&#39;createip&#39;))->select();
     $this->assign(&#39;title&#39;,&#39;thinkphp视频演示&#39;);
     $this->assign(&#39;alist&#39;,$list);
     $this->display();     
   }
   //删除
   function del(){
     $user=D(&#39;user&#39;);
     if($user->delete($_GET[&#39;id&#39;])){
       $this->success(&#39;删除成功&#39;);   
     }else{
       $this->error(&#39;删除失败&#39;);
     }
   }
   //增加
   function add(){
     Load(&#39;extend&#39;);
     if($_POST[&#39;password&#39;]!=$_POST[&#39;repassword&#39;]){
       $this->error(&#39;两次密码不一致&#39;);   
     }
     $user=D(&#39;user&#39;);
     if($vo=$user->create()){
       $user->password=md5($user->password);
       $user->createtime=time();
       //$user->createip=$_SERVER[];
       $user->createip=get_client_ip();
       if($user->add()){
         $this->success(&#39;用户注册成功,返回上级页面&#39;);   
       }else{
         $this->error(&#39;用户注册失败,返回上级页面&#39;);
       }
     }else{
       $this->error($user->getError());   
     }
   }
   //显示用户的修改项
   function edit(){
     $user=M(&#39;user&#39;);
     $id=(int)$_GET[&#39;id&#39;];
     $list=$user->where("id=$id")->find();
     $this->assign(&#39;data&#39;,$list);
     $this->assign(&#39;title&#39;,&#39;显示用户编辑信息&#39;); 
     $this->display();
   }
   //将更新数据写入数据库
   function update(){
     $user=M(&#39;user&#39;);
     $user->password=md5($user->password);
     if($user->create()){
       if($insertid=$user->save()){
         $this->success(&#39;更新成功,受影响的行数为&#39;.$insertid);
       }else{
         $this->error(&#39;更新失败&#39;);   
       }
     }
   }
}
?>
Copy after login

Interested readers can debug and run the examples described in this article in the project to deepen their understanding of The understanding of ThinkPHP's addition, deletion, check and modification operations can be used flexibly in future projects.

Related recommendations:

ThinkPHP string functions and common functions summary

ThinkPHP implements simple login function

The above is the detailed content of ThinkPHP basic add, delete, check and modify operation example tutorial. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template