我是新來的codeIgniter,我跟着主要教程,現在我試圖創建更新功能,但它給了我一個錯誤,我不知道爲什麼。更新數據庫[codeIgniter]
這裏是我的模型
public function update_news($id=0)
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'),'dash',TRUE);
$data = array(
'title' => $this->input->post('title'),
'text' => $this->input->post('text')
);
$this->db->where('id', $id);
return $this->db->update('news', $data);
}
控制器:
public function update($id)
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Editar noticia';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/update');
$this->load->view('templates/footer');
}
else
{
$this->news_model->update_news($id);
$this->load->view('news/success');
}
}
查看
<?php
function showComplete()
{
echo site_url('news/'.$news_item['slug']);
}
?>
<?php foreach ($news as $news_item): ?>
<h3><?php echo $news_item['title']; ?></h3>
<div class="main">
<?php echo $news_item['text']; ?>
</div>
<p>
<a href="<?php echo site_url('news/'.$news_item['slug']); ?>" class="btn btn-primary">Ver Noticia</a>
<a href="<?php echo site_url('news/update/'.$news_item['id']); ?>" class="btn btn-warning">Actualizar</a>
<a href="<?php echo site_url('news/delete/'.$news_item['id']); ?>" class="btn btn-danger">Borrar</a>
</p>
<?php endforeach; ?>
<a href="<?php echo site_url('news/create'); ?>" class="btn btn-default"> Nueva noticia</a>
------編輯-------
現在我沒有錯誤,感謝球員,但現在我認爲我的更新頁面中的form_open缺少變量傳遞,我怎樣才能讓表單傳遞ID到我的模型?
更新視圖
<h2><?php echo $title; ?></h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/update/'); ?>
<div class="col-md-8">
<label for="title">Title</label>
<input type="input" name="title" class="form-control"> <br>
<label for="text">Text</label>
<textarea class="form-control" name="text" id="" cols="30" rows="10"></textarea> <br>
<input class="btn btn-primary" type="submit" name="submit" value="Create new item">
</div>
</form>
仍給我同樣的錯誤,缺少的參數和Id未定義的變量,作爲一個說明,我從index.php這樣調用這個'「class =」btn btn-warning「> ...' –
您是否獲得了$ id值? –