【发布时间】:2012-09-12 13:22:01
【问题描述】:
我正在使用 uri 段来删除我的数据库中的信息:
anchor('site/delete_note/'.$row->id, 'Delete')
型号:
function delete_note()
{
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('note');
}
它工作正常,但我想更新我的信息,但无法让它工作 所以这是视图中的链接:
anchor('site/edit_note/'.$row->id, 'Edit')
我的控制器:
function edit_note()
{
$note_id = $this->uri->segment(3);
$data['main_content'] = 'edit_note';
$this->load->view('includes/template', $data);
$this->load->library('form_validation');
$this->form_validation->set_rules('content', 'Message', 'trim|required');
if($this->form_validation->run() == TRUE)
{
$this->load->model('Note_model');
$this->Note_model->edit_note($note_id);
redirect('site/members_area');
}
}
我的模特:
function edit_note($note_id)
{
$content = $this->input->post('content');
$data = array('content' => $content);
$this->db->where('id', $note_id);
$this->db->update('note', $data);
}
我对edit_note的看法:
<?php
echo form_open('site/edit_note');
echo form_textarea('content', set_value('content', 'Your message'));
echo form_submit('submit', 'Change');
echo anchor('site/members_area', 'Cancel');
echo validation_errors('<p class="error">'); ?>
当我尝试直接在编辑模型中获取段时,编辑不能作为删除工作,就像我在删除模型中使用的那样。
如果我在控制器中将 $note_id 设置为一个数字,而不是这个 '$this->uri->segment(3)',它会更新我的数据库。但是,如果我使用获取细分它不起作用。我认为 uri 段在控制器中与模型中一样可用,但有一些我不知道。
【问题讨论】:
-
这应该可以正常工作,我在您的代码中看不到任何错误
-
遗憾的是它没有 :( 就像我说的,如果我将这个 "$note_id = $this->uri->segment(3)" 更改为 "$note_id = 24" (我在我的控制器中使用我数据库中存在的 id。所以获取段有问题
-
查看我的答案和答案中的编辑
标签: codeigniter uri segment