简体   繁体   中英

How to edit MySql database record in Codeigniter?

I would like to add simple edit functionality which unfortunately was not covered in tut,I would like to have an edit button that opens a form and update info.
I have followed tutorial from here: CRUD TUTORIAL

Here is my Controller:

<?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();
     }

}

Here is my Model:

<?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');

       }


}

Here is my View:

<!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>

It's look seems like there is no option form submit to Edit data table in your view. Try this.

Controller

<?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();
  }

}

Create new view (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>

Edit your model. Target ID asset to update

<?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');

       }


}

Edit your options_view.php to add Edit link in you action table

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM