• 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");
        }
  • 相关阅读:
    js数组的用法以及数组根据下标(数值或字符)移除元素
    组件创建、组件注册方式
    vue.runtime.esm.js:593 [Vue warn]: Invalid prop: custom validator check failed for prop "value".报错解决
    uni-app 子组件如何调用父组件的方法
    wap2app(十)--wap2app 添加原生底部导航,添加原生标题栏,填坑
    wap2app(九)-- 使用mui.previewImage之后,页面a链接不能跳转
    wap2app(八)-- iphoneX 底部导航的兼容问题
    wap2app(七)-- 长按保存图片
    我的新书《Android App开发从入门到精通》终于出版啦
    Android微信登录、分享、支付
  • 原文地址:https://www.cnblogs.com/yy01/p/5725299.html
Copyright © 2020-2023  润新知