2017-06-26 212 views
0

我想添加簡單的編輯功能,但不幸的是沒有涵蓋在嘖嘖,我想有一個編輯按鈕,打開窗體和更新信息。
我按照教程從這裏: CRUD TUTORIAL如何編輯Codeigniter中的MySql數據庫記錄?

這裏是我的控制器:

<?php 

class Site extends CI_Controller 
{ 

    function index() 
    { 

     $data = array(); 

     if($query = $this->site_model->get_records()) 
     { 
      $data['records'] = $query; 
     } 
     //$this->load->library('table'); 
     $this->load->view('options_view',$data); 
    } 

    function create() 
    { 
     $data = array(
      'title' => $this->input->post('title'), 
      'content' => $this->input->post('content') 
      ); 

     $this->site_model->add_record($data);  
     $this->index(); 
    } 

    function update() 
    { 
     $data = array (
      'title' => 'My NEW UPDATED title', 
      'content' => 'NEW UPDATED content; UPDATED' 
      ); 
     $this->site_model->update_record($data); 
    } 




    function delete() 
    { 
     $this->site_model->delete_row(); 
     $this->index(); 
    } 

} 

這裏是我的模型:

<?php 

class Site_model extends CI_Model { 

    function get_records() 
    { 
     $query = $this->db->get('assets'); 
     return $query->result(); 
     // $query = $this->db->query('SELECT * FROM assets'); 
     //echo $this->table->generate($query); 

    } 


    function add_record($data) 
    { 
     $this->db->insert('assets', $data); 
     return; 
    } 

    function update_record($data) 
    { 
     $this->db->where('id', 3); 
     $this->db->update('assets', $data); 
    } 



    function delete_row() 
     { 
     $this->db->where('id', $this->uri->segment(3)); 
     $this->db->delete('assets'); 

     } 


} 

這是我的看法:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Untitled</title> 
    <style type="text/css" media="screen"> 
     label{display:block;} 
    </style> 
</head> 
<body> 

<h2>Create</h2> 
<?php echo form_open('site/create');?> 

<p> 

<label for="title">Title:</label> 
<input type="text" name="title" id="title" /> 

</p> 

<p> 

<label for="content">Content:</label> 
<input type="text" name="content" id="content" /> 

</p> 

<p> 

    <input type="submit" value="submit" /> 

</p> 




    <?echo form_close(); ?> 

     <hr /> 





     Read 
     <table> 
     <?php if(isset($records)) : foreach ($records as $row) : ?> 
    <tr> 
    <td> 
    <?php echo anchor("site/delete/$row->Id", $row->title); ?> 
    <td> 
    <td><?php echo $row->content; ?> </td> 
    <tr> 
     <td></td><td></td><td></td><td></td><td>edit</td> 
    </tr> 

    </tr> 

    <?php endforeach; ?> 
    </table> 

    <?php else : ?> 

    <h2>No records returned.</h2> 

    <?php endif; ?> 




    <hr /> 

    <h2>Delete</h2> 

<p>To sample the delete method, click on on of the headings above. 
A delete query will automatically run. 
</p> 

</body> 
</html> 
+1

您聯繫本教程是從2009年(8年前!),並使用代碼點火器v1。我做了一個快速谷歌,並有很多代碼點火器3的CRUD示例,這是更近的。遵循編程教程時,請始終檢查日期。 – hdifen

+0

現在還是一樣。您可以在Codeigniter官方文檔中查看它。 –

+0

從現在起8小時後,我會給你答案代碼。因爲我現在真的很困。 –

回答

0

這看起來好像沒有選項表單提交到您的視圖中的編輯數據表。嘗試這個。

控制器

<?php 

class Site extends CI_Controller 
{ 

    function index(){ 
    $data = array(); 
    if($query = $this->site_model->get_records()){ 
     $data['records'] = $query; 
    } 
     $arr = $_SERVER['REQUEST_URI']; 
     if (preg_match('#[0-9]#',$arr)){ 
     $questionmark = explode('?', $arr); 
     $number = $questionmark[1]; 
     if(is_numeric ($number)){ 
      $this->load->view('edit_record'); 
     }else{ 
      //LOAD TABLE (because ? without no ?id) 
      $this->load->view('options_view',$data); 
     } 
     }else{ 
      //LOAD TABLE (becuase there is no ?id) 
      $this->load->view('options_view',$data); 
     } 

    } // end index 

    function create() 
    { 
    $data = array(
     'title' => $this->input->post('title'), 
     'content' => $this->input->post('content') 
    ); 

    $this->site_model->add_record($data);  
    $this->index(); 
    } 

    function update(){ 
    $id = $this->input->post('id'); // send id to model 
     $data['title'] = $this->input->post('title'); 
     $data['content'] = $this->input->post('content'); 
    $this->site_model->update_record($data, $id); 
    } 




    function delete() 
    { 
    $this->site_model->delete_row(); 
    $this->index(); 
    } 

} 

創建新視圖(edit_record.php)

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Untitled</title> 
    <style type="text/css" media="screen"> 
    label{display:block;} 
    </style> 
</head> 
<body> 

    <h2>EDIT</h2> 
    <?php 
    $arr = $_SERVER['REQUEST_URI']; 
    $questionmark = explode('?', $arr); 
    $id = $questionmark[1]; 
    $query = $this->db->get_where('assets', array('id' => $id)); 
    if($query->result()){ 
    echo form_open('site/update'); 
    foreach ($query->result() as $row) { 
     ?> 

     <p> 
     <label for="title">Title:</label> 
     <input type="text" name="title" id="title" value="<?php echo $row->title; ?>" /> 
     </p> 

     <p> 
     <label for="content">Content:</label> 
     <input type="text" name="content" id="content" value="<?php echo $row->content; ?>" /> 
     </p> 

     <input type="hidden" name="id" value="<?php echo $id; ?>" /> 

     <p> 
     <input type="submit" value="UPDATE" /> 
     </p> 

     <?php 
    } 
    echo form_close(); 
    } else { 
    echo 'no record found with id <b>'. $id .'</b>'; 
    } 
    ?> 
</body> 

編輯您的模型。目標ID的資產更新

<?php 

class Site_model extends CI_Model { 

    function get_records() 
    { 
     $query = $this->db->get('assets'); 
     return $query->result(); 
     // $query = $this->db->query('SELECT * FROM assets'); 
     //echo $this->table->generate($query); 

    } 


    function add_record($data) 
    { 
     $this->db->insert('assets', $data); 
     return; 
    } 

    function update_record($data, $id) 
    { 
     $this->db->where('id', $id); // here is the id 
     $this->db->update('assets', $data); 
     redirect(site_url('site')); //redirect after done update process 
    } 



    function delete_row() 
     { 
     $this->db->where('id', $this->uri->segment(3)); 
     $this->db->delete('assets'); 

     } 


} 

編輯您的options_view.php添加在你的行動表編輯鏈接

<?php echo anchor('site?'. $row->Id, 'EDIT'); ?> 
+0

我的榮幸@excelsiorone。所以你有嘗試嗎?它工作嗎?如果有問題,請告訴我。我只是盡我所能。很高興幫助 –

+0

謝謝Kamarul,感謝您的幫助和時間。我做了一切,但仍沒有更新現有記錄。無論如何,你可以幫助我,或者我可以發送文件給你審查?再次感謝!! – excelsiorone

+0

@excelsiorone問題在哪裏?有錯誤訊息? –