0801 am使用tp框架对数据库增删改查

增添数据,3种方法                        

function Text3()
{
$m=D("info");
//1.使用数组
$attr = array(
"code"=>"p005",
"name"=>"李师师",
"sex"=>true,
"nation"=>"c004",
"brithday"=>"1198-9-6",
);
$r=$m->add($attr);
dump($r);
} function Text2()
{
$m = D("info");
//2.VR方式
$m->code="p008";
$m->name="和谐";
$m->sex=0;
$m->nation="n003";
$m->brithday="1998-5-21";
$r=$m->add();
//直接调用该方法写参数,方式是数组的,没有参数,方式是VR的
dump($r);
}

  

//3.自动收集表单
$n=D("nation");
$attr=$n->select();
$this->assign("nation",$attr); if(empty($_POST))
{
$this->display();
}
else
{
$m = D("info");
$m->create();//自动收集数据,name一定和列名对应起来
$m->sex=$_POST["sex"]==0?false:true;
$r=$m->add();
dump($r);
if($r)
{
$this->success("添加成功","Text",10);//提示信息,跳转页面
}
else
{
$this->error("添加失败","Text",3);
}
}
}
//Text.html 显示页面代码,在view文件夹下创建Text目录;
<body>
<form action="__ACTION__" method="post">
<div>代号<input type="text" name="code"></div>
<div>姓名<input type="text" name="name"></div>
<div>性别
<input type="radio" name="sex" checked="checked" value="1">男
<input type="radio" name="sex" value="0">女
</div>
<div>民族
<select name="nation">
<foreach name="nation" item="v">
<option value="<{$v.code}>"><{$v.name}></option>
</foreach>
</select>
</div>
<div>生日<input type="text" name="brithday"></div>
<div><input type="submit" value="提交"></div>
</form>
</body>

  修改                         

//TextController.class.php 代码
function XiuGai()
{
$n=D("nation");
$attr=$n->select();
$this->assign("nation",$attr);
$code=$_GET["code"];
$m=D("info");
if(empty($_POST))
{
$attr=$m->find($code);
var_dump($attr);
$this->assign("info",$attr);
$this->display();
}
else
{
$m->create();
$m->sex=$_POST["sex"]==0?false:true;
$m->save();
}
}
//XiuGai.html 显示页面代码,在view文件夹下创建XiuGai目录;
<body>
<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 eq 1" >
<input type="radio" name="sex" checked="checked">男
<input type="radio" name="sex">女
<else />
<input type="radio" name="sex">男
<input type="radio" name="sex" checked="checked">女
</if>
</div>
<div>民族 <{$info.nation}>
<select name="nation">
<foreach name="nation" item="v">
<if condition=" $info.nation eq $v.code ">【内置标签If】
<option value="<{$v.code}>" selected="selected"><{$v.name}></option>
<else />
<option value="<{$v.code}>"><{$v.name}></option>
</if>
</foreach>
</select>
</div>
<div>生日<input type="text" name="brithday" value="<{$info.brithday}>"></div>
<div><input type="submit" value="提交" id="btn"></div>
</form>
<input type="button" id="aa" value="哈哈哈">
</body>

  删除                      

function ShanChu()
{
$m=D("info");
//$m->where("code='p088'")->delete();
//$m->where("nation='n001'")->delete();
$m->delete("p003");
}

 

上一篇:vscode 配置踩坑记


下一篇:learning ddr RTT