tp框架之增删改查

控制器

<?php
namespace HomeController;
use ThinkController;
class CeShiController extends Controller
{
    public function tianjia()
    {
        if(empty($_POST))
        {
            //如果没有传值过来,显示页面
            $this->display();
        }
        else
        {
            //如果提交了值过来
            $n = D("Nation");
            $n->create();//自动收集表单.前提是必须有post数据
            
            $z = $n->add();
            
            if($z)
            {
                $this->success("添加成功","test",5);  //5秒之后跳回test
            }
            else
            {
                $this->error("添加失败!");
            }
        }    
    }
    
    public function test()
    {
        //内容的显示页面
        $m = D("Nation");    
        $attr = $m->select();
        
        $this->assign("m",$attr);
        
        $this->display();
    }
    
    function xiugai($code="")  //默认值为空,防止报错
    {
        $m = D("Nation");
        
        if(empty($_POST))
        {
            //如果没有提交表单,就根据传过来的code查出内容并显示
            $attr = $m->find($code);
            $this->assign("m",$attr);
            $this->display();    
        }    
        else
        {
            //根据提交的表单内容进行修改并跳回
            $m->create();
            $r = $m->save();    
            if($r)
            {
                $this->success("修改成功","test",5);
            }
            else
            {
                $this->error("修改失败!");
            }
        }
    }
    
    public function shanchu($code)
    {
        $m = D("Nation");    
        $r = $m->delete($code);
        if($r)
        {
            $url = U("test");
            $this->success("删除成功",$url,5);
        }
        else
        {
            $this->error("删除失败!");
        }
    }
}
View Code

添加页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<form action="__ACTION__" method="post">
<input type="text" name="Code" />
<input type="text" name="Name" />
<input type="submit" value="提交" />
</form>
</body>
</html>
View Code

修改页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<form action="__ACTION__" method="post">
    <div><input type="hidden" name="Code"  value="<{$m.code}>"/></div>
    <div>名称:<input type="text" name="Name" value="<{$m.name}>" /></div>

    <input type="submit" value="修改" />
</form>
</body>
</html>
View Code

显示页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td>代号</td>
        <td>名称</td>
        <td>修改</td>
        <td>删除</td>
    </tr>
    <foreach name="m" item="v" >
    <tr>
        <td><{$v.code}></td>
        <td><{$v.name}></td>
        <td><a href="__CONTROLLER__/xiugai/code/<{$v.code}>">修改</a></td>
        <td><a href="__CONTROLLER__/shanchu/code/<{$v.code}>" onclick="return confirm('确定删除吗?')">删除</a></td>
    </tr>
    </foreach>
</table>

<a href="__CONTROLLER__/tianjia">添加</a>


</body>
</html>
View Code
原文地址:https://www.cnblogs.com/bilibiliganbei/p/6214477.html