ThinkPHP增删改

1.使用数组
$attr=array("Code"=>"p008","Name"=>"张三","Sex"=>true,"Nation"=>"n001","Birthday"=>"2000-8-8");
$m->add($attr);

2.使用AR方式 把m看做一条数据
$m->Code="p009";
$m->Name="李四";
$m->Sex=0;
$m->Nation="n002";
$m->Birthday="2001-8-8";
$m->add();

3.自动收集表单
$m->create();
$bs = $m->add();

if ($bs)
{
$this->success("添加成功", "Test");  第一个参数为完成后的提示  第二个参数是添加完成后跳转到的页面 还可以加第三个参数为过多久跳转
} else
{
$this->error("添加失败");  失败默认跳转到上一个页面
}

  发现不存在的列会自动忽略
  如果数据类型不正确 默认输入0

  修改必须有主键值

class TestController extends Controller
{
    function Test()
    {
        if (empty($_POST))
        {
            $this->display();
        }
        else
        {
            $m = D("info");
            //1.使用数组
            //$attr=array("Code"=>"p008","Name"=>"张三","Sex"=>true,"Nation"=>"n001","Birthday"=>"2000-8-8");
            //$m->add($attr);

            //2.使用AR方式 把m看做一条数据
            //$m->Code="p009";
            //$m->Name="李四";
            //$m->Sex=0;
            //$m->Nation="n002";
            //$m->Birthday="2001-8-8";
            //$m->add();

            //3.自动收集表单
            $m->create();
            $bs = $m->add();
            if ($bs)
            {
                $this->success("添加成功", "Test");
            } else
            {
                $this->error("添加失败");
            }
        }
    }

    function XiuGai()
    {
        $code=$_GET["code"];
        $m=D("info");
        if(empty($_POST))
        {
            $attr=$m->find($code);
            $this->assign("info",$attr);
            $this->display();
        }
        else
        {
            $m->create();
            $m->save();
        }
    }
    function Delete()
    {
        $m=D("info");
        //$m->where("Code='p012'")->delete();//通过where加条件,删多条
        $m->delete("p011");//添加一个主键值,删一条
    }
}

  添加数据页面  注意 name值应该跟表的列名一样 注意大小写

<form action="__ACTION__" method="post">
    <div>代号:<input type="text" name="Code" /></div>
    <div>姓名:<input type="text" name="Name" /></div>
    <div>性别:<input type="text" name="Sex" /></div>
    <div>民族:<input type="text" name="Nation" /></div>
    <div>生日:<input type="text" name="Birthday" /></div>
    <div><input type="submit" value="添加" /> </div>
</form>

  修改数据页面   注意<{$info.name}>此时应为小写

<form action="__ACTION__" method="post">
    <input type="hidden" name="Code" value="<{$info.code}>" />
    <div>姓名:<input type="text" name="Name" value="<{$info.name}>" /></div>
    <div>性别:<input type="text" name="Sex" value="<{$info.sex}>" /></div>
    <div>民族:<input type="text" name="Nation" value="<{$info.nation}>" /></div>
    <div>生日:<input type="text" name="Birthday" value="<{$info.birthday}>" /></div>
    <div><input type="submit" value="修改" /> </div>
</form>

  Tips :使用TP框架是 数据库表名 最好用小写

               //$this->error();注释掉这个 避免添加失败的bug

 

posted @ 2016-08-01 19:50  格调evo  阅读(157)  评论(0编辑  收藏  举报