添加数据(注意表名大小写)
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
修改(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
删除(delete)
function ShanChu() { $model=D("Info"); //1.根据where条件删除多条 $model->where("Code='p083'")->delete(); //2.$model->delete("主键值");删除一条数据 $model->delete("p082"); }