ThinkPHP Basic Addition Deletion Checking and Modifying Operation Example Tutorial

  • 2021-07-13 04:58:09
  • OfStack

This paper describes the basic operation of adding, deleting, checking and modifying ThinkPHP, which is the most commonly used basic operation in ThinkPHP project development and has 10 important application values. Now I will share the complete example code with you, hoping to help you. 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 section

1. User homepage template:

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


<form action="__URL__/add" method="post">
  User name :<input type="text" name="username" /><br />
  Password: <input type="password" name="password" /><br />
  Duplicate password: <input type="repassword" name="repassword" /><br />
 <input type="submit" value=" Registration " />
</form>

<volist name="alist" id="vo">
 <li><span>ID : </span>{$vo['id']}<span> User name: </span>{$vo['username']}<span> Registration ip : </span>{$vo['createip']}<a href="__URL__/del/id/{$vo['id']}"> Delete </a>&nbsp;&nbsp;<a href="__URL__/edit/id/{$vo['id']}"> Edit </a></li>
</volist>

2. User editing templates:

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


<form action="__URL__/update" method="post">
  User name :<input type="text" name="username" value="{$data['username']}" /><br />
  Password: <input type="password" name="password" value="{$data['password']}" /><br />
 IP : <input type="text" name="createip" value="{$data['createip']}" /><br />
  Time: <input type="text" name="createtime" value="{$data['createtime']}" /><br />
 <input type="hidden" value="{$data['id']}" name="id" />
 <input type="submit" value=" Update " />
</form>

2. action Part:

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


class UserAction extends Action {
   function index(){
     $user=M('user');
     $list=$user->field(array('id','username','createip'))->select();
     $this->assign('title','thinkphp Video presentation ');
     $this->assign('alist',$list);
     $this->display();     
   }
   // Delete 
   function del(){
     $user=D('user');
     if($user->delete($_GET['id'])){
       $this->success(' Delete succeeded ');   
     }else{
       $this->error(' Delete failed ');
     }
   }
   // Increase 
   function add(){
     Load('extend');
     if($_POST['password']!=$_POST['repassword']){
       $this->error(' Twice password no 1 To ');   
     }
     $user=D('user');
     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(' The user registered successfully and returned to the superior page ');   
       }else{
         $this->error(' User registration failed, return to superior page ');
       }
     }else{
       $this->error($user->getError());   
     }
   }
   // Display user's modifications 
   function edit(){
     $user=M('user');
     $id=(int)$_GET['id'];
     $list=$user->where("id=$id")->find();
     $this->assign('data',$list);
     $this->assign('title',' Display user editing information '); 
     $this->display();
   }
   // Write update data to the database 
   function update(){
     $user=M('user');
     $user->password=md5($user->password);
     if($user->create()){
       if($insertid=$user->save()){
         $this->success(' Update succeeded , The number of rows affected is '.$insertid);
       }else{
         $this->error(' Update failed ');   
       }
     }
   }
}
?>

Interested readers can debug and run the examples described in this article in the project, so as to deepen the understanding of ThinkPHP addition, deletion, search and modification operation, and facilitate flexible application in future projects.


Related articles: