PHP CURD实现的中$_GET和$_POST用法的小问题

来源:互联网 发布:java int 除法取整 编辑:程序博客网 时间:2024/06/06 08:29

1.controller调用Index/index.php

 public function index(){        $db = M('Zheng');                            // 实例化模型类,参数数据表名称,不包含前缀        $select = $db->order('id desc')->limit(10)->select();        $this->assign('select',$select);            // 模板变量赋值        $this->display();                           // 指定模板页    }

2.display定位到视图view下index.html

<table width="805" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF">  <tr>    <td colspan="4" bgcolor="#FFFFFF" class="title" align="center">用户信息</td>  </tr>  <tr class="title">    <td bgcolor="#FFFFFF" width="44">ID</td>    <td bgcolor="#FFFFFF" width="120">名称</td>    <td bgcolor="#FFFFFF" width="223">地址</td>    <td bgcolor="#FFFFFF" width="223"> 操作 </td>  </tr>  </tr>  <foreach name='select' item='user' >  <tr class="content">    <td bgcolor="#FFFFFF">&nbsp;{$user.id}</td>    <td bgcolor="#FFFFFF">&nbsp;{$user.user}</td>    <td bgcolor="#FFFFFF">&nbsp;{$user.address}</td>    <td bgcolor="#FFFFFF"><a href="__URL__/update?id={$user.id}">更新</a>&nbsp;&nbsp;<a href="__URL__/delete?id={$user.id}">删除</a></td>  </tr>  </foreach></table>

页面显示:
这里写图片描述
3.enter“更新”,定位到index.php下的update方法

 public function update(){        $db = M('Zheng');             $select = $db->where('id='.$_GET['id'])->select();            $this->assign('select',$select);            $this->display(update);        if(isset($_POST['id'])){             $data['user'] = $_POST['user'];              // 要修改的数据对象属性赋值            $data['pass'] = md5($_POST['pass']);            $data['address'] = $_POST['address'];                       $result = $db->where('id='.$_POST['id'])->save($data);        // 根据条件保存修改的数据            if($result){                $this->redirect('Index/index','', 0.2,'数据更新成功');      //页面重定向             } }    }

4.定位到update.html页面

<table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF">  <tr>    <td colspan="2" bgcolor="#FFFFFF" class="title" align="center">用户信息</td>  </tr> <foreach name='select' item='user' >  <tr class="content">    <td bgcolor="#FFFFFF" class="right" width="103">名称:</td>    <td bgcolor="#FFFFFF" width="289"> <input type="hidden" name="id" id="hiddenField" value="{$user.id}" /><input name="user" type="text" id="user" size="20" value="{$user.user}" /></td>    </tr>  <tr class="content">    <td bgcolor="#FFFFFF" class="right">密码:</td>    <td bgcolor="#FFFFFF"><input name="pass" type="password" id="pass" size="20" value="{$user.pass}" />     </td>    </tr>  <tr class="content">    <td bgcolor="#FFFFFF" class="right">&nbsp;地址:</td>    <td bgcolor="#FFFFFF">&nbsp;      <input name="address" type="text" id="address" size="30" value="{$user.address}" />    </td>    </tr>  <tr class="content">    <td bgcolor="#FFFFFF">&nbsp;</td>    <td bgcolor="#FFFFFF"><input type="submit" name="button" id="button" value="更新" /></td>  </tr> </foreach></table>

页面显示:
这里写图片描述
5.enter“更新”,显示的页面
这里写图片描述
今天主要要说的小问题就是这个,看了他的提示,以为是数据库出现了语句语法错误,找了无功而返,接着看到“select”第一次可以select到数据库的数据,但是更新select具体对应id的数据时却找不到,所以怀疑是第二次执行update方法时GET[id]getupdateisset_GET[‘id’])判断语句,果然就不会出错 了,update方法的代码修改如下:

 public function update(){        $db = M('Zheng');        if(isset($_GET['id'])){                                    //isset()判断可以使update.html传过来的post数据不用执行这一段代码,否则就会出错,因为get不到$_GET的数据            $select = $db->where('id='.$_GET['id'])->select();            $this->assign('select',$select);            $this->display(update);        }        if(isset($_POST['id'])){          //  var_dump($_POST['id']);            $data['user'] = $_POST['user'];              // 要修改的数据对象属性赋值            $data['pass'] = md5($_POST['pass']);            $data['address'] = $_POST['address'];                       $result = $db->where('id='.$_POST['id'])->save($data);        // 根据条件保存修改的数据            if($result){                $this->redirect('Index/index','', 0.2,'数据更新成功');      //页面重定向             }        }    }
1 0