thinkphp 添加 修改删除

在 MainController.class.php  

添加

public function zhuCe()
    {
        
         //时间两个逻辑
         // 1 显示页面 2向数据库添加
           if(empty($_POST))
           {
                
             $this->show(); //显示页面
           }
           else 
           {
             $n = D("Nation");
             $n->create();//自动收集信息 前提是必须有POST 数据
             $z=$n->add();  //添加数据
             if($z)
             {
                 $this->success("添加成功","zhuCe",6);
                  
             }
             else
             {
                $this->error("添加失败","zhuCe",6); 
             }
           }
        
    }

修改

<!-- __CONTROLLER__ 获取该控制地址器信息-->

显示    public function test()
    {
        //数据访问
        //造模型对象        
        $nation = D("Nation");        
        //查询
        $a = $nation->select(); //查所有,返回关联数组              
        $this->assign("nation",$a);    //注入
        $this->show();             //显示 test 静态页面
    }

静态页面代码


修改页面代码

修改操作

public function xiugai($code="") // 给$code 一个空默认值 否则会报错
{
$n=D("Nation");    
if(empty($_POST))
{
$nation=$n->find($code); 根据传过来的CODE值 单一查找这一项内容
$this->assign("nation",$nation); 把查询内容注入
$this->show();  显示到修改页面
}
else
{
$n->create();//搜集信息
$r=$n->save(); //执行修改 返回布尔型
if($r)
{
$this->success("修改成功","test");
}
else
{
$this->error();
}
}
}

删除

    public function shanchu($code)
    {
        $n=D("nation");
        $r=$n->delete("$code");
        if($r)
        {  
           $testa=U("test"); //获取页面地址
            $this->success("删除成功",$testa);
        }
        else
        {
            $this->error("失败");
        }
            
    }
原文地址:https://www.cnblogs.com/benpaodegegen/p/6215494.html