Codeigniter:似乎无法让我的编辑工作

发布于 2024-12-07 05:36:11 字数 3339 浏览 0 评论 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';

So basically I am trying to combine what I found in these two tutorials to make a working CRUD:
http://fwebde.com/php/simple-crud-with-codeigniter/

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

Basically, where I am is that I can get the edit page to render the appropriate item details in the form but whenever I press submit, I get a 404 error and the table doesn't get updated.

My edit.php that is tied to my news controller:

<?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>

My news controller on the edit method:

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');
    }
}

Appropriate methods in my model:

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);
}

My routes:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小猫一只 2024-12-14 05:36:11

您正在将表单提交到 edit 方法,该方法需要一个项目 ID $slug,您可以将视图更改为以下内容:

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

You are submitting the form to the edit method which is expecting an item id $slug, you can change your view to the following:

<?php echo form_open('news/edit/' . $news_item['id']) ?>
原谅我要高飞 2024-12-14 05:36:11

基本上有几个错误:

1)ifaour的建议

2)将主键和slug作为参数传递以供稍后使用

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);   
    }
}

Basically there were a couple of errors:

1) ifaour's suggestion

2) passing the primary key and slug around as parameters to be used for later

3) forgot I had changed the table column (forgot I had renamed the content column as text)

Model in its final form:

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);
}

Edit in its final form:

<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>

news controller edit method in final form:

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);   
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文