我正在使用codeigniter,我試圖更新新聞文章,當用戶單擊更新按鈕時,我通過使用id = $ this-> uri從url獲取id - >段(3);當點擊更新按鈕時,它會加載一個名爲update的視圖。我遇到的問題是變量不是將我的id傳遞給模型,因爲當我提交表單時,id不再存儲在$ id變量中。主要讚賞任何幫助。當提交表單時,變量會丟失ID
新聞控制器
public function update() {
$id = $this->uri->segment(3);
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Update a news item';
$this->load->view('news/update');
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
if ($this->form_validation->run())
{
$this->News_model->update($id);
redirect('News/index');
}
}
新聞模型
public function update($id) {
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);
$this->db->where('id',$id);
$this->db->update('news',$data);
}
主要新聞頁面爲什麼要使用
<h1><?php echo $title; ?></h1>
<p class="lead"><a href="<?php echo site_url('News/create'); ?>">Create Article</a></p>;
</div>
<?php foreach ($news as $news_item): ?>
<h3><?php echo $news_item['title']; ?></h3>
<div class="row">
<div class="col-xs-12 col-sm-6 col-lg-6"> <?php echo $news_item['text']; ?></div>
<div class="col-xs-6 col-lg-2"><p><a href="<?php echo site_url('news/'.$news_item['id']); ?>">View article</a></p></div>
<div class="col-xs-6 col-lg-2"><p><a href="<?php echo site_url('News/update/'.$news_item['id']); ?>">update</a></p></div>
<div class="col-xs-12 col-sm-6 col-lg-2"><p><a href="<?php echo site_url('News/delete/'.$news_item['id']); ?>">Delete article</a></p></div>
</div>
<?php endforeach; ?>
我認爲你必須將'$ id'的值保留在隱藏字段中。而當表單發佈收集該值並將其傳遞給模型時! – deepakb