2011-09-28 97 views
0

所以基本上我想結合我發現在這兩個教程做一個工作的CRUD: http://fwebde.com/php/simple-crud-with-codeigniter/笨:似乎無法讓我的編輯工作

http://ie.mirror.twsweb-int.com/codeigniter/user_guide/tutorial/create_news_items.html

基本上,我在那裏我的意思是我可以通過編輯頁面來在表單中呈現相應的項目詳細信息,但每當我按下提交時,就會得到一個404錯誤,並且表格不會更新。

我edit.php被綁定到我的新聞控制器:

<?php echo validation_errors(); ?> 
<h2>Edit a news item</h2> 

<?php echo form_open('news/edit') ?> 

<p> 
<label for="title">Title</label> 
<?php echo form_input('title',$news_item['title']); ?> 
</p> 

<p> 
<label for="text">Text</label> 
<?php echo form_textarea('text',$news_item['text']); ?> 
</p>  

<?php echo form_hidden($news_item['id']); ?> 

<p> 
<?php echo form_submit('submit', 'Save Changes'); ?> 
</p> 

    <?php echo form_close(); ?> 

</br> 
</br> 
<a href='<?php echo site_url('news');?>'>Back</a> 

的編輯方法,我的新聞控制器:在我的模型

public function edit($slug) 
{ 
    $data['news_item'] = $this->news_model->get_news($slug); 

    if (empty($data['news_item'])) 
    { 
     show_404(); 
    } 

    $data['title'] = 'Edit: '.$data['news_item']['title']; 

    $this->load->helper('form'); 
    $this->load->library('form_validation'); 

    $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/edit', $data); 
     $this->load->view('templates/footer'); 
    } 
    else 
    { 
     $this->news_model->update_news($this->input->post('id'), 
             $this->input->post('title'), 
             $this->input->post('text')); 
     $this->load->view('news/success'); 
    } 
} 

合適的方法:

public function get_news($slug = FALSE) 
{ 
    if ($slug === FALSE) 
    { 
     $query = $this->db->get('news'); 
     return $query->result_array(); 
    } 

    $query = $this->db->get_where('news', array('slug' => $slug)); 
    return $query->row_array(); 
} 

public function set_news() 
{ 
    $this->load->helper('url'); 

    $slug = url_title($this->input->post('title'), 'dash', TRUE); 

    $data = array(
     'title' => $this->input->post('title'), 
     'slug' => $slug, 
     'text' => $this->input->post('text') 
    ); 

    return $this->db->insert('news', $data); 
} 

public function update_news($id, $title, $content) { 
    $data = array(
     'title' => $title, 
     'content' => $content 
    ); 

    $this->db->where('id', $id); 
    $this->db->update('news', $data); 
} 

我的路線:

$route['news/edit/(:any)'] = 'news/edit/$1'; 
$route['news/create'] = 'news/create'; 
$route['news/(:any)'] = 'news/view/$1'; 
$route['news'] = 'news'; 

回答

2

基本上有一對夫婦的錯誤:

1 )ifaour的建議

2)使主鍵和蛞蝓圍繞作爲參數將被用於以後

3)忘了我已經改變了表列(忘了我有改名的最終形式的內容列文本)

型號:

public function get_news($slug = FALSE) 
{ 
    if ($slug === FALSE) 
    { 
     $query = $this->db->get('news'); 
     return $query->result_array(); 
    } 

    $query = $this->db->get_where('news', array('slug' => $slug)); 
    return $query->row_array(); 
} 

public function set_news() 
{ 
    $this->load->helper('url'); 

    $slug = url_title($this->input->post('title'), 'dash', TRUE); 

    $data = array(
     'title' => $this->input->post('title'), 
     'slug' => $slug, 
     'text' => $this->input->post('text') 
    ); 

    return $this->db->insert('news', $data); 
} 

public function update_news($id, $title, $content) { 
    $data = array(
     'title' => $title, 
     'text' => $content 
    ); 

    $this->db->where('id', $id); 
    $this->db->update('news', $data); 
} 

編輯的最終形式:

<h2>Edit a news item</h2> 

<a href="<?php echo site_url('news/delete/'.$news_item['id']); ?>">Delete this article</a> 

<?php echo validation_errors(); ?> 

<?php echo form_open('news/edit/'.$news_item['slug']) ?> 

<p> 
<label for="title">Title</label> 
<?php echo form_input('title',$news_item['title']); ?> 
</p> 

<p> 
<label for="text">Text</label> 
<?php echo form_textarea('text',$news_item['text']); ?> 
</p>  

<?php echo form_hidden('slug',$news_item['slug']); ?> 
<?php echo form_hidden('id',$news_item['id']); ?> 

<p> 
<?php echo form_submit('submit', 'Save Changes'); ?> 
</p> 

<?php echo form_close(); ?> 

</br> 
</br> 
<a href='<?php echo site_url('news');?>'>Back</a> 

最終形式的新聞控制器編輯方法:

public function edit($slug) 
{ 
    $data['news_item'] = $this->news_model->get_news($slug); 

    if (empty($data['news_item'])) 
    { 
     show_404(); 
    } 

    $data['title'] = 'Edit: '.$data['news_item']['title']; 

    $this->load->helper('form'); 
    $this->load->library('form_validation'); 

    $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/edit', $data); 
     $this->load->view('templates/footer'); 
    } 
    else 
    { 
     $this->news_model->update_news($this->input->post('id'), 
             $this->input->post('title'), 
             $this->input->post('text')); 


     $data['news_item'] = $this->news_model->get_news($slug); 
     $this->load->view('templates/header', $data);         
     $this->load->view('news/success'); 
     $this->load->view('news/edit', $data); 
     $this->load->view('templates/header', $data); 
    } 
} 
2

所提交的形式向edit方法,期待一個項目ID $slug,您可以更改您的視圖爲以下內容:

<?php echo form_open('news/edit/' . $news_item['id']) ?> 
+0

我的解決方案的一部分,但最終成爲更多。不過謝謝你的洞察力。 – Zigu