用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1972|回复: 2
收起左侧

修改页面怎么获取列表页面的数据

[复制链接]
发表于 2014-1-3 17:19:55 | 显示全部楼层 |阅读模式
最近新学PHP,用的是CI框架,在练习一个用户注册登录CRUD操作的模块,
现遇到一个问题把列表页面的数据传到修改页面
[backcolor=rgba(255, 255, 255, 0.8)]列表页面如下:
<html>
<head>
    <title><?php echo $title;?></title>
    <script type="text/javascript">
        
    </script>
</head>
<body>
    <div style="background-color: blue;color:white;
    text-align: center;font-size: 30px" ><?php echo $headLine;?></div>
    <form id="admin"
        action="http://dev.test.cn/index.php/admin"
         name="admin" method="post">
    <table    align="center">
        <tr style="background-color: blue;color:white;">
            <td width="10%">id</td>
            <td width="10%">用户名</td>
            <td width="10%">密码</td>
            <td    width="10%">性别</td>
            <td width="15%">email</td>
            <td    width="10%">电话</td>
            <td width="15%">地址</td>
            <td width="25%">编辑</td>
        </tr>
        <?php foreach ($users as $user):?>
            <tr>
                <td width="10%" id="id" ><?php echo $user->id;?></td>
                <td width="10%"><?php echo $user->name;?></td>
                <td    width="10%"><?php echo $user->pwd;?></td>
                <td    width="10%"><?php echo $user->sex;?></td>
                <td width="10%"><?php echo $user->email;?></td>
                <td    width="10%"><?php echo $user->phone;?></td>
                <td width="10%"><?php echo $user->address;?></td>
                <td width="25%">
                    <input type="button" value="删除"
                         id="delete" name="delete"/>
                    <a  id="load"
                    >修改</a>
                </td>
            </tr>
        <?php endforeach;?>
    </table>
    </form>
</body>
</html>

修改页面如下:
<html>
<head>
    <title>修改用户信息</title>
</head>
<body>
    <div style="background-color: blue;color:white;
    text-align: center;font-size: 30px" >修改用户信息</div>
    <p align="center">用户信息(*为必填项)</p>
    <form    name="registerForm" action=
    "http://dev.test.cn/index.php/update/update"   
        method="post">
    <?php
    foreach($query as $row) : ?>
        <table align="center">
            <tr>
            <input type="hidden" name="id" value="<?php echo $row->id;?>" />
                <td>用户名:</td>
                <td><input type="text" name="name"
                    id="name" value="<?php echo $row->name;?>" >*</td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="password" name="pwd"
                    id="pwd" value="<?php echo $row->pwd;?>">*</td>
            </tr>
            <tr>
                <td>验证密码:</td>
                <td><input type="password" name="v_pwd"
                    id="v_pwd" value="<?php echo $row->pwd;?>">*</td>
            </tr>
            <tr>
                <td>性别:</td>
                <td><input type="text" name="sex"
                    id="sex" value="<?php echo $row->sex;?>">
                </td>   
            </tr>
            <tr>
                <td>email:</td>
                <td><input type="text" name="eamil"
                    id="email" value="<?php echo $row->email;?>"></td>
            </tr>
            <tr>
                <td>联系电话:</td>
                <td><input type="text" name="phone"
                    id="phone" value="<?php echo $row->phone;?>"></td>
            </tr>
            <tr>
                <td>地址:</td>
                <td><input type="text" name="adress"
                    id="adress" value="<?php echo $row->address;?>"></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" name="submit"
                    id="submit" value="修改">
                    <input type="button" name="return"
                  
                        value="返回"></td>
            </tr>
        </table>
    </form>
<?php endforeach;?>
</body>
</html>
结果老是显示Trying to get property of non-object

修改的Controller如下
class Update extends CI_Controller{
        function __construct(){
            parent::__construct();        
        }
        function index(){
            $this->load->helper("form");
            $data['title']="User Update";
            $data['headLine']="用户修改页面";
            //$this->load->vars($data);
            $this->load->helper("url");
            $id = $this->uri->segment(4);
            $this->load->model("mtest","",TRUE);
            $data['query']=$this->mtest->getOne($id);
            $this->load->view("user/update",$data);
        }
        function update(){
            $this->load->helper("url");
            $this->load->model("mtest","",TRUE);
            $data=array(
                'name'=> $this->input->post('name'),
                'pwd'=>$this->input->post('pwd'),
                'sex'=>$this->input->post('sex'),
                'email'=>$this->input->post('email'),
                'phone'=>$this->input->post('phone'),
                'address'=>$this->input->post('address'));
            $this->mtest->update($this->input->post('id'),$data);
            redirect("admin","refresh");
        }
    }

修改的Model如下
function update($id,$data){
             $this->db->where("id",$id);
             $query=$this->db->update("user",$data);
             $result=$query->result();
             return $result;
         }
求高手指正,到底哪里错了?




发表于 2014-1-3 19:59:50 | 显示全部楼层
在列表页面,把条目ID值以参数形式带到修改页面   例如 : edit/111   ,在修改页面通过ID查询内容,赋值到视图
 楼主| 发表于 2014-1-3 23:38:38 | 显示全部楼层
能说的详细点吗?谢谢

本版积分规则