如何编辑Codeigniter中的MySql数据库记录?

问题描述:

我想添加简单的编辑功能,但不幸的是没有涵盖在啧啧,我想有一个编辑按钮,打开窗体和更新信息。
我按照教程从这里: 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小时后,我会给你答案代码。因为我现在真的很困。 –

这看起来好像没有选项表单提交到您的视图中的编辑数据表。尝试这个。

控制器

<?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问题在哪里?有错误讯息? –