使用代码点火器中的 ID 更新特定记录


Updating specific records by using a id in codeigniter

我是codeigniter的新手。我需要使用 id 更新记录。 我尽了自己的一份力量,但这不是更新。我附上了下面的代码。我还需要更新图像。我对更新图像感到困惑。也请帮助我更新图像。如果您考虑这个问题,我将很高兴。

"我的视图"页面

      <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <h1 class="page-header">Update Article</h1>
            <?php foreach ($article as $row ) {
            }?>
           <form action="<?php echo base_url();?>index.php/article/update_save" method="POST" enctype="multipart/form-data" role="form">
              <div class="form-group">
                <label for="title">Title</label>
                <input type="text" class="form-control" name="title" value="<?php echo $row->art_title; ?>">
                <label for="name">Description</label>
                <textarea cols="30" rows="10" class="form-control" name="desc"><?php echo $row->art_desc; ?></textarea>
                <label for="title">Image</label>
                <a href="#" class="thumbnail">
                <img src="<?php echo base_url();?>assests/img/<?php echo $row->art_img; ?>"/>
                </a>
                <input type="file" class="form-control" name="upload">
                <br/>
                <div align="right">
                  <input type="submit" class="btn btn-primary" value="Update">
                  <input type="reset" class="btn btn-danger" value="Reset">
                </div>
              </div>
            </form>
        </div> 

这是我的控制器

function update_article(){
    $id=$this->uri->segment(3);
    $data = array();
    $data['article'] = $this->article_m->show_update_m($id);
    $this->load->view('update_article', $data);
}
function update_save(){
    $id=$this->uri->segment(3);
    $data_update = array(
        'art_title' => $this->input->post('title'),
        'art_desc' => $this->input->post('desc')
        );
    //print_r($data_update);
    $this->article_m->do_update_m($id, $data_update);
    $this->edit_article();
}

这是我的模型

function show_update_m($id){
    $this->db->select('*');
    $this->db->from('front_article');
    $this->db->where('art_id' ,$id);
    $query=$this->db->get();
    return $query->result();
}

function do_update_m($id, $data_update){
    $this->db->where('art_id', $id);
    $this->db->update('front_article', $data_update);
}

正如我在您的代码中看到的那样,您没有从视图中发送 ID:

<form action="<?php echo base_url();?>**index.php/article/update_save**" method="POST" enctype="multipart/form-data" role="form">

那么控制器中的$id是什么?

$id=$this->uri->segment(3);

试试这个:将其放在您的表单中:

<input type="hidden" name="id" value="<?=$row->id?>" />

并通过以下方式获取控制器中的 ID:

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