简体   繁体   中英

How to update data from a table given the ID using codeigniter?

I'm trying to make an update function in my form with codeigniter. This is what I have so far

controller

public function editMenu()
  {
    $data['title'] = 'Menu Management';
    $data['user'] = $this->db->get_where('user', ['email' =>
    $this->session->userdata('email')])->row_array();

    $data['menu'] = $this->db->get('user_menu')->result_array();

    $this->form_validation->set_rules('menu', 'Menu', 'required');

    if($this->form_validation->run() == false){
      //Must be in order
      $this->load->view('templates/header', $data);
      $this->load->view('templates/sidebar', $data);
      $this->load->view('templates/topbar', $data);
      $this->load->view('menu/index', $data);
      $this->load->view('templates/footer');
    }

    else
    {
      $menu = $this->input->post('menu');
      $id = $this->input->post('id');

      $this->db->set('menu', $menu);
      $this->db->where('user_menu'. '.' .'id', $id);
      $this->db->update('user_menu');

      $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">updated</div>');
      redirect('menu');
    }
  }

View

<div class="modal fade" id="editMenuModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="editMenuModal">Edit Menu</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <form action="<?= base_url('menu/editMenu'); ?>" method="post">
        <div class="modal-body">
          <p style="color:green">Enter new name</p>
          <input type="text" class="form-control mt-3" id="id" name="menu" value="<?= $m['id'] ?>" readonly>
          <div class="form-group">
            <input type="text" class="form-control" id="email" name="menu" value="<?= $m['menu'] ?>">
          </div>
        </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Edit</button>
      </div>
     </form>
    </div>
  </div>
</div>

The edit doesn't seem to work even though the updated message is shown after I finish inserting the new name. The data name stays the same. When I try to echo the query, it is giving me back UPDATE `user_menu` SET `menu` = 'd' WHERE `user_menu`.`id` IS NULL

I wanted to take in the ID from the form in the view code and pass it into the controller, where it is then used to execute the query

您输入的id名称错误,请将name="menu"更改为name="id"

        <input type="text" class="form-control mt-3" id="id" name="id" value="<?= $m['id'] ?>" readonly>

Do two things

Change the name attribute of input having id="id"

<input type="text" class="form-control mt-3" id="id" name="id" value="<?= $m['id'] ?>" readonly>

Change the update code

No need to use the table name here. But if you want to use do like this $this->db->where('user_menu.id', $id);

$menu = $this->input->post('menu');
$id = $this->input->post('id');

$this->db->set('menu', $menu);
$this->db->where('id', $id);
$this->db->update('user_menu');

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