2017-08-31 72 views
0

View頁面,在這裏ID不能在這裏得到解析顯示一些錯誤likeMessage:未定義的變量:行& &消息:試圖讓非對象的屬性「ID」不會被解析的笨視圖控制器

<form action="<?php echo site_url('Insert_ctrl/update/'.$row->id.'');?>" method="post"> 


<?php 
foreach ($g as $row) 
{ 

?> 
<label>ID</label> 
<input type="text" name="id" value="<?php echo $row->id;?>"><br> 
<label>Name</label> 
<input type="text" name="dname" value="<?php echo $row->student_name;?>"><br> 

這裏是我的控制器頁面

function edit($id) 
    { 
     $data['g'] ="";   
     $this->load->database();     
     $this->load->model('Inserts_model'); 
     $data['g'] =$this->Inserts_model->select_row($id); 
     $this->load->view('update_view', $data); 
    } 

這裏是我的模型

public function select_row($id) 
{ 
    $this->db->where('id', $id); 
    $query = $this->db->get('student'); 
    return $query->result_object(); 
} 

這裏是另一個控制器功能更新即那張表格後得到執行

function update($id) 
     { 
       if(isset($_POST['update'])) 
        { 
         $data = array 
         (
         'student_name' => $this->input->post('dname'), 
         'student_email' => $this->input->post('demail'), 
         'student_mobile' => $this->input->post('dmobile'), 
         'student_address' => $this->input->post('daddress') 
         ); 
         $this->Inserts_model->update($data,$id); 
         redirect('Insert_ctrl/index'); 
        } 
     } 
+0

我們可以看到闕在模型中執行功能? – goseo

+0

在你的控制器中echo'$ id'並檢查。 'echo $ id;退出;' –

回答

0

每當你嘗試從數據庫中獲得單個記錄,那麼你應該使用row(),而不是result()

public function select_row($id) 
{ 
    $this->db->where('id', $id); 
    $query = $this->db->get('student'); 
    return $query->row(); //use row() instead of result 
} 

你的方法名是edit和您提交update方法

<form action="<?php echo site_url('Insert_ctrl/edit/'.$g[0]->id.'');?>" method="post"> 

的形式,如果你是路過idinput,那麼你並不需要與URL

送你應該嘗試與此$this->uri->segment(3);

+0

請通過上面編輯的代碼 –

+0

@jibinkj我已經更新我的回答 –

+0

@jibinkj你可以通過print_r()來顯示你的'$ g' foreach()' –

0

,如果你要查詢單行,那麼你最好使用row_array()

return $query->row_array(); 

,然後你就可以訪問數組的索引,如:

echo $row['id']; 
+0

不,返回$ query-> result_object();在模型頁 –