Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 代码点火器:Can';我的编辑似乎无法正常工作_Php_Codeigniter_Crud - Fatal编程技术网

Php 代码点火器:Can';我的编辑似乎无法正常工作

Php 代码点火器:Can';我的编辑似乎无法正常工作,php,codeigniter,crud,Php,Codeigniter,Crud,因此,基本上,我正在尝试结合我在这两个教程中发现的内容来制作一个工作积垢: 基本上,我所处的位置是,我可以让编辑页面在表单中呈现适当的项目详细信息,但每当我按submit时,就会出现404错误,并且表不会得到更新 绑定到我的新闻控制器的My edit.php: <?php echo validation_errors(); ?> <h2>Edit a news item</h2> <?php echo form_open('news/edit')

因此,基本上,我正在尝试结合我在这两个教程中发现的内容来制作一个工作积垢:

基本上,我所处的位置是,我可以让编辑页面在表单中呈现适当的项目详细信息,但每当我按submit时,就会出现404错误,并且表不会得到更新

绑定到我的新闻控制器的My 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 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';

如果要将表单提交到
编辑
方法,该方法需要项目id
$slug
,则可以将视图更改为以下内容:

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

如果要将表单提交到
编辑
方法,该方法需要项目id
$slug
,则可以将视图更改为以下内容:

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

基本上有几个错误:

1) 我们的建议

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);   
    }
}
以最终形式编辑:

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

基本上有几个错误:

1) 我们的建议

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);   
    }
}
以最终形式编辑:

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

我的解决方案的一部分,但结果是更多。谢谢你的洞察力。这是我的解决方案的一部分,但最终会有更多。谢谢你的洞察力。