使用编码点火器编辑记录


edit records using codeigniter

我在编辑数据库中的记录时遇到问题。我有一个表格,显示了视图页面中所有 cliends 的记录,在它们旁边我有一个锚链接来编辑记录。

我将 id 发送到名为 site/update 的控制器函数,并使用

 $id = $this->uri->segment(3);

然后我将 id 发送到 findRecord($id) 模型,以获取数据库中与 id 对应的记录。

如果我print_r查询,我得到

 Array ( [records] => Array ( [0] => stdClass Object ( [id] => 2 [name] =>
 sdf [surname] => sdf [contact] => 35353 [email] => blabla@gmail.com ) ) ) 

我可以在控制器中打印它,而不是在视图中打印它

我该怎么做?问题出在哪里?

控制器代码:

    <?php
    class Site extends CI_Controller 
    {
        function __construct()
        {
            parent::__Construct();
            $this->is_logged_in();
        }
        function members_area()
        {
            $data=array();
            if($query=$this->site_model->getrecords()){
                $data['records']=$query;
            }
            $this->load->view('logged_in_area',$data);
        }
        function add() // just for sample
        {
            $this->load->view('add_clients');
        }
        function is_logged_in()
        {
            $is_logged_in = $this->session->userdata('is_logged_in');
            if(!isset($is_logged_in) || $is_logged_in != true)
            {
                echo 'You don''t have permission to access this page. <a href="../login">Login</a>';    
                die();      
                //$this->load->view('login_form');
            }       
        }
        function create(){
                $this->load->library('form_validation');///// form validation to add clients
            // field name, error message, validation rules
            $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
            $this->form_validation->set_rules('surname', 'Surname', 'trim|required|xss_clean');
            $this->form_validation->set_rules('contact', 'Contact Number', 'trim|required|min_length[4]');
            $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
            if($this->form_validation->run() == FALSE)
            {
                $this->load->view('add_clients');
            }else{
                $data=array('name'=>$this->input->post('name'),
                'surname'=>$this->input->post('surname'),
                'contact'=>$this->input->post('contact'),
                'email'=>$this->input->post('email'));
                $this->site_model->addRecords($data);
                $this->members_area();  
                }
        }
        function delete($id){
                $this->site_model->deleteRow($id);
                $this->members_area();
        }
        function update(){
            $data2=array();
            $id = $this->uri->segment(3);
        if($query=$this->site_model->findRecord($id)){
            $data2['records']=$query;
            //print_r($data2);
            $this->load->view('logged_in_area',$data2);
                }else{echo' nothing';}
            //$this->load->model('employesModel');
            //if($t=$this->site_model->findRecord($id)){
            //$this->load->view('update_clients',$data);
            //$this->site_model->updateRecord($data,$id);
        }
    }

视图模型

    <body>
        <h2 style="text-align:left">Welcome Back, <?php echo $this->session->userdata('username'); ?>!</h2>
         <p>This section represents the area that only logged in members can access.</p><br/>
         <h2>Clients List</h2>
         <?php
         if(isset($records)):
         echo'<table align="left"  width="40%" cellpadding="6" cellspacing="3" border="0">
         <tr><td align="left"><b>Name</b></td><td align="left"><b>Surname</b></td><td align="left"><b>Phone number</b></td><td align="left"><b>Email address</b></td><td>
         </td></tr>';
          foreach ($records as $row): ?>
        <tr>
            <td><?php echo $row->name;?></td>
            <td><?php echo $row->surname;?></td>
            <td><?php echo $row->contact;?></td>
            <td><?php echo $row->email;?></td>
            <td align="center"><?php echo anchor("site/delete/$row->id",'Delete');?>&nbsp;<?php echo anchor("site/members_area/$row->id",'Update')?></td>
        </tr>
          <?php endforeach; echo '</table>'?>
          <?php else: ?>
          <h2>No records were returned</h2>
          <?php endif; ?><br/><br/><br/><br/><?php if(isset($data2))var_dump($data2);?><br/><br/>
    <p>
         <?php echo anchor('site/add','Add clients');?>&nbsp;
         <?php echo anchor('login/logout', 'Logout'); ?>
    </p>
    </body>
    </html> 

最后是模型

function findRecord($id){
    $query = $this->db->get_where('clients', array('id' => $id));
    return  $query->result();
   }
 }

您没有获取任何数据来查看文件,因为您没有将数据发送到该文件。看看你的代码,你有一个拼写错误:

if($query**=**$this->site_model->getrecords()){
            $data['records']=$query;}

尝试将 = 更改为 ==。

看起来您正在接收数组中的数组,请尝试使用数组的索引 $records['0'] 而不是 $records。

E.g: $records['0'] as $row