• PHP TP增删改


    添加数据(注意表名大小写)

    1.数组方式(add)

    $model=D("Info");    
    $info=array("Code"=>"p080","Name"=>"学习","Sex"=>true,"Nation"=>"n001","Birthday"=>"1998-2-3");
    $model->add($info);

    2.AR方式(给成员赋值后直接调用add方法)

    $model=D("Info");    
    $model->Code="p081";
    $model->Name="学习1";
    $model->Sex=0;
    $model->Nation="n001";
    $model->Birthday="1998-2-3";
    $model->add();

    3.自动收集表单(create)

    if(empty($_POST))
            {
                $nation=D("Nation");
                $attr=$nation->select();
                $this->assign("attr",$attr);
                $this->display();
                
            }
            else
            {
                $model=D("Info");    
                $rules=array(
                array("Code","require","代号不能为空",0,"regex",3),
            );
                
                if(!$model->validate($rules)->create())
                {
                    echo $model->getError();
                }
                else
                {
                    $model->Sex=$_POST["Sex"]=="1"?true:false;
                    $bs=$model->add();
                    //跳转页面
                    //1.成功后跳转:success("提示的话","跳转的操作方法",等待的时长)
                    //2.失败后跳转:error("提示的话","默认跳回上一个页面")
                    if($bs)
                    {
                        $this->success("添加成功","test");
                    }
                    else
                    {
                        $this->error("添加失败");
                    }
                }
            }

    test.html

    <form action="__ACTION__" method="post">
        <div>代号:<input id="dh" type="text" name="Code" /></div>
        <span id='dhyz'></span>
        <div>姓名:<input type="text" name="Name" /></div>
        <div>性别:男<input type="radio" name="Sex" value="1" /><input type="radio" name="Sex" value="0" /></div>
        <div>民族:
            <select name="Nation">
                <foreach name="attr" item="v">
                        <option value="<{$v.code}>"><{$v.name}></option>
                </foreach>
            </select>
        </div>
        <div>生日:<input type="text" name="Birthday" /></div>
        <div><input type="submit" value="提交" /></div>
    </form>

    修改(save)

    function XiuGai()
        {
            //修改(save)
            $code=$_GET["code"];
            $model=D("Info");
            $nation=D("Nation");
            if(empty($_POST))
            {
                $info=$model->find($code);
                $nations=$nation->select();
                $names=$info["nation"];
                $this->assign("nation",$nations);
                $this->assign("info",$info);
                $this->display();    
            }
            else
            {
                $model->create();
                $model->save();
            }
        }

    xiugai.html

    <form action="__ACTION__" method="post">
        <input type="hidden" name="Code" value="<{$info.code}>" />
        <div>姓名:<input type="text" name="Name" value="<{$info.name}>" /></div>
        <div>性别:
            <if condition="$info.sex == 1"><input type="radio" name="Sex" value="1" checked="checked" /><input type="radio" name="Sex" value="0" />
            <else /><input type="radio" name="Sex" value="1" /><input type="radio" name="Sex" value="0" checked="checked" />
            </if>
        </div>
        <div>民族:
            <select name="Nation">
                <foreach  name="nation" item="v">
                    <if condition="$v['code'] == $info['nation']">
                        <option selected="selected" value="<{$v.code}>">
                            <{$v.name}>
                        </option>
                    <else />
                        <option value="<{$v.code}>">
                            <{$v.name}>
                        </option>
                    </if>
                </foreach >
            </select>
        </div>
        <div>生日:<input type="text" name="Birthday" value="<{$info.birthday}>" /></div>
        <div><input type="submit" value="修改" /></div>
    </form>

    删除(delete)

    function ShanChu()
        {
            $model=D("Info");
            
            //1.根据where条件删除多条
            $model->where("Code='p083'")->delete();
            //2.$model->delete("主键值");删除一条数据
            $model->delete("p082");
        }
  • 相关阅读:
    php八种常用函数
    已知二叉树的前序中序遍历,如何得到它的后序遍历?
    PTA_Have fun with numbers(C++)
    PTA_输入符号及符号个数打印沙漏(C++)
    Web安全之SQL注入
    南京邮电大学//bugkuCTF部分writeup
    修改或添加HTTP请求头
    第二次作业
    博客作业1
    linux python 串口
  • 原文地址:https://www.cnblogs.com/yy01/p/5725299.html
Copyright © 2020-2023  润新知