php - 更新时尝试获取 CodeIgniter 中非对象的属性

标签 php mysql codeigniter

我正在尝试制作更新表单,它将检索所选特定 ID 的数据,并填写表单,以便其可用于更新。 按下按钮后,我收到错误 “ undefined variable 结果”“尝试获取非对象的属性”

以下是我的代码。

主视图

   <form action="<?php echo base_url();?>index.php/validation_control/addnew" method="post">

        <table align="center" border=2>
            <tr>
                <th>id</th>
                <th>Username</th>
                <th>Firstname</th>
                <th>Lastname</th>
                <th>Image</th>
                <th>Email</th>
                <th>Birt Date</th>
                <th>Status</th>
                <th>Edit</th>
            </tr>            
            <?php
                foreach ($query as $row) 
                {
                    $style='';
                    if($row->status=='deactive'){$style = 'style="text-decoration: line-through"';}
                ?>  

                     <tr <?php echo $style;?>>
                        <td><?php echo $row->id; ?></td>
                        <td><?php echo $row->username; ?></td>
                        <td><?php echo $row->fname; ?></td>
                        <td><?php echo $row->lname; ?></td>
                        <td><?php echo "<img src='../codeigniter/uploads/".$row->userfile."' width=30 height=30>";?></td>
                        <td><?php echo $row->email; ?></td>
                        <td><?php echo $row->bdate; ?></td>                        
                        <td>
                        <a href="<?php echo base_url('index.php/validation_control/update_status');?>/<?php echo $row->id;?>/<?php echo $row->status; ?>" ">
                        <?php echo $row->status;?></a>
                        </td>

                         <td><a href="<?php echo base_url('index.php/validation_control/edit');?>/<?php echo $row->id;?>">Edit</a></td>

                    </tr>
                    <?php 
                } 
            ?>
                    <tr><td align="center" colspan="8"><input type="submit" value="Add New" /> </td></tr>
                    </table>    

   </form>

编辑 View

<html><body>
 <?php //echo form_open_multipart("validation_control/edit_member/");?>

       <form action="<?php echo base_url(); ?>index.php/validation_control/edit_member/"  method="post" id="email_form" onsumbit="return myFunction()">
            <table align="center" border="2">


                <tr><td>Username :-</td> <td><input type="text" name='uname' value="<?php echo $result[0]->username;?>"/><?php echo form_error('uname'); ?></td></tr>
                <tr><td>First Name :- <td><input type="text" name='fname' value="<?php echo $result[0]->fname;?>" /><?php echo form_error('fname'); ?></td></tr>
                <tr><td>Last Name :- <td><input type="text" name='lname' value="<?php echo $result[0]->lname;?>"/><?php echo form_error('lname'); ?></td></tr>
                <tr><td>Phone Number :- <td><input type="text" name='phone' value="<?php echo $result[0]->phone;?>"/><?php echo form_error('phone'); ?></td></tr>
                <tr><td>Email :- <td><input type="text" name='email' value="<?php echo $result[0]->email;?>"?><?php echo form_error('email'); ?></td></td></tr>
                <tr><td>Password :-<td><input type="password" name='pass' id='pass' value="<?php echo $result[0]->password;?>"/><?php echo form_error('pass'); ?></td></tr>
                <tr><td>Confirm Password :-<td><input type="password" name='cnfpass' id='cnfpass' value="<?php echo set_value('cnfpass'); ?>"/><?php echo form_error('cnfpass'); ?></td></td></tr>
                <tr><td>Birth Date :-<td><input type="datetime-local" name="date" ></td></td></tr>
                <tr><td>Image :- <td><input type="file" name="userfile" id="userfile" size="60" accept="image/*" value="<?php echo $result[0]->userfile;?>"></td>

                <tr><td>Status :-<td><input type="radio" name="status" value="active" />Active<input type="radio" name="status" value="deactive" />Deactive<input type="radio" name="status" value="delete" />Delete<?php //echo form_error('status'); ?></td></tr>



                <tr><td align="center" colspan="2"><input type="submit" name="sbmt" id="sbmt" value="Update" ></td></tr>


            </table>
        </form>
</body></html>

控制

    function edit($member_id)
     {
        $member = $this->validation_model->get_member($member_id);
        //$this->load->view('validation_edit', $member);
        $this->load->view('validation_edit',array('result'=>$member));
     }
    function edit_member($member_id=NULL)
     {
        //$product = $this->validation_model->get_member($member_id);

//      $this->data['title'] = 'Edit Member';

        //validate form input

        $this->form_validation->set_rules('uname', 'Username', 'required|is_unique[validation.username]');
        $this->form_validation->set_rules('fname', 'Firstname', 'required|min_length[2]');
        $this->form_validation->set_rules('lname', 'Lastname', 'required|min_length[2]');
        $this->form_validation->set_rules('phone', 'Phonenumber', 'required|max_length[10]|is_natural|valid_base64');
        $this->form_validation->set_rules('pass', 'Password', 'required');
        $this->form_validation->set_rules('cnfpass', 'Password Confirmation', 'required|matches[pass]');
        $this->form_validation->set_rules('status', 'Status', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
//  echo "calling"; exit;
            if ($this->form_validation->run() == FALSE)
            {
                $this->load->view('validation_edit');
            }
            else
            {   
            $image_path = $this->upload->data();
                        $data = array(

                            'username'=>$this->input->post('uname'),                        
                            'fname'=>$this->input->post('fname'),
                            'lname'=>$this->input->post('lname'),
                            'phone'=>$this->input->post('phone'),
                            'email'=>$this->input->post('email'),
                            'password'=>$this->input->post('pass'),
                            'bdate'=>$this->input->post('date'),
                            'status'=>$this->input->post('status'),
                            'userfile'=>$_FILES['userfile']['name']

                            //'userfile' => $image_path[name]
                            );
                $this->validation_model->update_member($data);
                echo $data;
            }
                //redirect();

//      $this->load->view('validation_edit', $this->data);
    }

型号

public function update_member($data)
{
    $this->db->where('id', $member_id);
    $this->db->update('validation', $data);
}

最佳答案

在你的 Controller 中尝试使用这个:

function edit($member_id)
 {
    $data['result'] = $this->validation_model->get_member($member_id);

    $this->load->view('validation_edit',$data);
 }

关于php - 更新时尝试获取 CodeIgniter 中非对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20655945/

相关文章:

php - Doctrine2 首先查询具有特定值的 orderBy

php - Exchange Web 服务请求在某些使用 PHP-EWS 的服务器上返回 401

php - SQL索引/关系

php - 在 mySQL 中选择一个由两个单词组成的字段名,从 PHP 中调用

mysql - 三重连接的问题

php - Codeigniter/PHP,2 个 Mysql 表,多个查询

php - CodeIgniter:第一个查询成功运行,第二个查询无法更改数据库。相同的值,不同的表

php - Jquery自动完成在一行中显示来自mysql的两行

php - 通过 shell_exec() 创建的视频无法播放,但通过终端创建的视频可以

mysql - 在单个查询中垂直堆叠 MySQL 结果