更新数据库[codeIgniter]

时间:2017-04-24 07:42:59

标签: php 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>

6 个答案:

答案 0 :(得分:2)

您必须将输入数据传递给更新新闻模型,始终遵循MVC结构。因为代码点火器是MVC框架。

public function update($id)
{
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->load->helper('url');

    $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
    {
        $slug = url_title($this->input->post('title'),'dash',TRUE); // i dont know about this slug. that you were using for what?
        $data = array(
           'title' => $this->input->post('title'),
           'text' => $this->input->post('text')
        );
        $this->news_model->update_news($id,$data);
        $this->load->view('news/success');
    }
}

,您的模型函数将如下所示

public function update_news($id,$data)
{
    $this->db->where('id', $id);
    return $this->db->update('news', $data);
}

答案 1 :(得分:2)

您的控制器缺少第二个参数,因此在&#39;新闻/更新&#39;中定义您的$数据,$ 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');
        $this->load->view('news/update',$data);
        $this->load->view('templates/footer');

    }
    else
    {

        $this->news_model->update_news($id);
        $this->load->view('news/success');
    }
}

答案 2 :(得分:1)

您可以查看我的更新代码

这是我的模特

public function update($id)
 {
    $username=$this->input->post('username');
    $password=$this->input->post('password');

    $data=array(
            'user'=> $username,
            'pass' => $password

    );

    $this -> db -> where('id', $id);
    $qq=$this->db->update('admin',$data);
    redirect('dashboard');
 }

这是我的控制器

public function edit($user_id)
{

    $this->db->where(['id'=>$user_id]);
    $data['result'] = $this->db->get('admin');
    $this->load->view("dashboard/edit", $data);

}

答案 3 :(得分:1)

您可能要求更新页面如下:

本地主机/控制器/更新

但更新方法需要额外的参数ID,因此正确的更新请求将是:

localhost / controller / update / 1

或者您可以通过声明更新的默认ID来避免错误:

public function update($id = 0) //  = 0 is default id assigning
{
    $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');
    }
}

答案 4 :(得分:1)

为您的&#39; application / config / routes.php&#39;

添加正确的路线
$route['news/update/(:num)'] = 'news/update/$1';

确保网址以下面的格式传递ID

/news/update/id

例如,  /news/update/1

答案 5 :(得分:0)

我想解决的错误很少。首先,您需要更正模型函数

<强>模型

public function update_news($id)
{
    $this->load->helper('url'); // Best practice to load any helper is in controller

    $slug = url_title($this->input->post('title'),'dash',TRUE); // No need to use this line in your model. If you are updating news slug then you can use it.

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

    $this->db->where('id', $id);

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

<强>控制器

定义$ id = 0或者不在函数中传递它,从URI字符串中选择。我更喜欢不直接在函数

中传递它
public function update()
{
    $this->load->helper('url');
    $id = trim($this->uri->segment(3)) != '' && is_numeric($this->uri->segment(3)) && $this->uri->segment(3) > 0 ? $this->uri->segment($this) : false;
    if($id == false){
        show_404();
    }

    $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())
    {
        $this->news_model->update_news($id);
        $this->session->set_flashdata('success', 'Yeah! You have updated news article successfully');
redirect(base_url('[TO_DESIRED_URL]')); // Here [TO_DESIRED_URL] denotes to you redirect to desired url after successful update.
    }
    $data['title'] = 'Editar noticia';
    $data['id'] = $id;
    $this->load->view('templates/header', $data);
    $this->load->view('news/update');
    $this->load->view('templates/footer');
}

由于您已解决显示文章列表问题,因此我在此处引用更新视图问题并在此处跳过一个视图。如果您对此也需要任何澄清,那么非常欢迎您。

<h2><?php echo $title; ?></h2>

<?php echo validation_errors(); ?>

// $id passed from controller.
<?php echo form_open('news/update/'.$id); ?>

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

我在这里详细解释过。如果您需要任何帮助,请告诉我。