How to implement data addition, deletion and modification using ThinkPHP framework

墨辰丷
Release: 2023-03-27 13:54:01
Original
1758 people have browsed it

The example in this article describes the operation method of add, delete, modify and query in thinkPHP database. Share it with everyone for your reference. I hope it will be helpful for everyone to learn to use thinkPHP

Using the TP framework is mainly simpler. Before we wrote additions, deletions and modifications, the amount of code was relatively large. It is very simple to use the tp framework to write here. , greatly reducing the amount of code

Here I am taking the nation table of the database as an example. The nation table has only two columns, one column is code and the other is name. First, add the add method

functinon add()
{
    if(empty($_POST))//首先要判断一下是否有传过来的值
         {
              //如果没有传过来的值,那么直接显示模板就可以,在view文件夹下的Main文件夹下建一个名为add.html的文件就可以了
             $this->show();
          }
      else
      {
        //如果有传过来的值,需要收集信息添加
         $n->D("nation");
         $n->create();
         if($n->add())
              {
                //已经添加成功了,需要跳转页面,跳转到一个方法
                $this->success("添加成功","add",3);
              }
        else
              {
                 $this->error("添加失败");
               }
      }
}
Copy after login

The second is the deletion method. The deletion method is relatively simple. Just pass a primary key value.

function del($code)
{
   $n=D("nation");
   $n->delete($code);
}
Copy after login

The last is the modification method. The modification method is relatively troublesome, because there will be a default value when we open the interface that needs to be modified

function update()
{
  $n=D("nation");
  $arr=$n->find($_GET["b"])
  if(empty($_POST))
    {
      $this->assign("arr",$arr); //这样在模板页面的input code的value值为{$arr.code} 同理input name的value值为{$arr.name}
      $this->show();
    }
   else
   {
      $n->create();
     if($n->save())
       {
         $this->success("修改成功","add",3);
       }
      else
       {
          $this->error("修改失败")
       }
   }
}
Copy after login


Related recommendations:

PHP implements the addition and deletion of xml Check operations Method

JavaScript DOM element additionDeleteDetailed steps

PHP is based on object-oriented mysqli Extension library additionDeletionCheck the method of operating tool class

The above is the detailed content of How to implement data addition, deletion and modification using ThinkPHP framework. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!