如何使用codeigniter更新具有给定ID的表中的数据?

时间:2019-04-25 21:11:45

标签: php codeigniter bootstrap-4 codeigniter-3

我正在尝试使用codeigniter在表单中进行更新。这就是我到目前为止

控制器

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

查看

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

即使我输入完新名称后仍显示已更新消息,该编辑似乎也不起作用。数据名称保持不变。当我尝试回显查询时,它给了我UPDATE `user_menu` SET `menu` = 'd' WHERE `user_menu`.`id` IS NULL

我想从视图代码的表单中获取ID,并将其传递给控制器​​,然后在控制器中使用该ID执行查询

2 个答案:

答案 0 :(得分:0)

您为(gdb) info b Num Type Disp Enb Address What 3 breakpoint keep y 0x00007f7cb1645c7c in QGuiApplicationPrivate::processMouseEvent(QWindowSystemInterfacePrivate::MouseEvent*) at kernel/qguiapplication.cpp:1699 stop only if (e->globalPos.xp < 1920 && e->buttons) breakpoint already hit 5 times (gdb) p e->globalPos.xp < 1920 && e->buttons Can't do that binary op on that type (gdb) p e->globalPos.xp < 1920 $6 = false 输入的名称错误,将id更改为name="menu"

name="id"

答案 1 :(得分:0)

做两件事

更改具有id="id"的输入的名称属性

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

更改更新代码

无需在此处使用表名。但是,如果您想使用$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');