我是新来的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值? –