1.添加html
<form action="{:U('Tel/add')}" method="post"> ?? ??? ?手机名称 <input type="text" name="mname" id=""><br> ?? ??? ?手机价格 <input type="number" name="mprice" id=""><br> ?? ??? ?所属品牌 <select name="bid"> ?? ??? ??? ??? ??? ?<option value="">--请选择品牌--</option> ?? ??? ??? ??? ??? ?<foreach name="data" item="vo"> ?? ??? ??? ??? ??? ??? ?<option value="{$vo.bid}">{$vo.bname}</option> ?? ??? ??? ??? ??? ?</foreach> ?? ??? ??? ??? ?</select><br> ?? ??? ??? ?<input type="submit" value="手机添加"> ?? ?</form>
2.修改.html?
?? ?<form action="{:U('Tel/update')}" method="post"> ?? ??? ?<!-- 使用隐藏域传id值到修改页面 --> ?? ??? ?<input type="hidden" name="id" value="{$telinfo.id}"> ?? ??? ?手机名称 <input type="text" name="mname" value="{$telinfo.mname}"><br> ?? ??? ?手机价格 <input type="number" name="mprice" value="{$telinfo.mprice}"><br>
?? ??? ?所属品牌 <select name="bid"> ?? ??? ??? ??? ??? ?<option value="">--请选择品牌--</option> ?? ??? ??? ??? ??? ?<foreach name="data" item="vo"> ?? ??? ??? ??? ??? ??? ?<eq name="vo.bid" value="$telinfo.bid"> ?? ??? ??? ??? ??? ??? ??? ?<option value="{$vo.bid}" selected >{$vo.bname}</option> ?? ??? ??? ??? ??? ??? ?<else/> ?? ??? ??? ??? ??? ??? ??? ?<option value="{$vo.bid}">{$vo.bname}</option> ?? ??? ??? ??? ??? ??? ?</eq> ?? ??? ??? ??? ??? ?</foreach> ?? ??? ??? ??? ?</select><br> ?? ??? ??? ?<input type="submit" value="手机修改"> ?? ?</form>
3.控制器
<?php? namespace Home\Controller; use Think\Controller; use Think\Request; class TelController extends Controller{ ?? ?public function create(){ ?? ??? ?// 查询品牌表中数据 ?? ??? ?$Brand = M("Brand"); ?? ??? ?$data = $Brand->select(); ?? ??? ?$this->assign("data",$data); ?? ??? ?$this->display(); ?? ?} ?? ?public function add(){ ?? ??? ?$Tel = M("Tel"); ?? ??? ?$data['bid'] = I("bid"); ?? ??? ?$data['mname'] = I("mname"); ?? ??? ?$data['mprice'] = I("mprice"); ?? ??? ?$data['mtime'] = date("Y-m-d h:i:s",time()); ?? ??? ?$res = $Tel->add($data); ?? ??? ?if($res){ ?? ??? ??? ?$this->success("添加成功",U("tel/show"));die; ?? ??? ?}else{ ?? ??? ??? ?$this->error("添加失败",U("tel/create"));die; ?? ??? ?} ?? ?} ?? ?//列表 ?? ?public function show(){ ?? ??? ?//两表联查 ?? ??? ?$Tel = M('Tel a'); ?? ??? ?$list = $Tel->join('brand b ON a.bid=b.bid')->select(); ?? ??? ?$this->assign("list",$list); ?? ??? ?$this->display(); ?? ?} ?? ?//删除 ? ?? ?public function delete(){ ?? ??? ?//接收id值 ?? ??? ?$id = I("id"); ?? ??? ?$Tel = M("Tel"); ?? ??? ?$res = $Tel->where("id=$id")->delete(); ?? ??? ?if($res){ ?? ??? ??? ?$this->success("删除成功",U("tel/show"));die; ?? ??? ?}else{ ?? ??? ??? ?$this->error("删除失败",U("tel/show"));die; ?? ??? ?} ?? ?} ?? ?// 编辑? ?? ?public function edit(){ ?? ??? ?$id = I("id"); ?? ??? ?$Tel = M("Tel"); ?? ??? ?$telinfo = $Tel->where("id=$id")->find();
?? ??? ?$this->assign("telinfo",$telinfo);
?? ??? ?// 查询品牌表中数据 ?? ??? ?$Brand = M("Brand"); ?? ??? ?$data = $Brand->select();
?? ??? ?$this->assign("data",$data); ?? ??? ?$this->display(); ?? ?} ?? ?//修改 ?? ?public function update(){ ?? ??? ?$Tel = M("Tel"); ? ??? ??? ?$id = I("id"); ? ??? ??? ?$data["bid"] = I("bid");?? ? ? ??? ??? ?$data["mname"] = I("mname");?? ? ? ??? ??? ?$data["mprice"] = I("mprice");?? ? ? ??? ??? ?$res = $Tel->where("id=$id")->save($data); ?? ??? ?if($res===1){ ?? ??? ??? ?$this->success("修改成功",U("show"));die; ?? ??? ?}else if($res===0){ ?? ??? ??? ?$this->error("数据未做修改",U("tel/edit/id/".$id));die; ?? ??? ?}else{ ?? ??? ??? ?$this->error("修改失败",U("tel/edit/id/".$id));die; ?? ??? ?} ?? ?} }
4.表?
?记录学习点滴,支持原创,希望对您有所帮助!
|