更新数据库[codeIgniter]

I'm new to codeIgniter, I have followed the main tutorial and now I'm trying to create the update funtion, but it gives to me an error that I don't know why.

Here is my model

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

Controller:

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

View

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

------edit-------

Now I do not have errors, thanks guys, but now I think that my form_open from my update page is missing the variable passing, how can I make that the form pass the ID to my model?

update view

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

you have to pass the data of your input to update news model , Always follow the MVC structure . because code igniter is MVC Framework.

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

and your model function will look like this

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

Your controller missing the second Parameter so define your $data in 'news/update',$data not in template/header

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

You can check my update code

This is my model

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

This is my controller

public function edit($user_id)
{

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

}

Probably you requested the update page like:

localhost/controller/update

But the update method required additional parameter ID, therefore correct request for update will be:

localhost/controller/update/1

Or you can avoid errors by declaring default ID for update:

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

Add proper route to your 'application/config/routes.php'

$route['news/update/(:num)'] = 'news/update/$1';

Make sure url is passing id in below format

/news/update/id

For instance, /news/update/1

There are few mistakes I would like to address. First of all you need to correct model function

Model

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

Controller

Define $id=0 or do not pass it in function, pick from URI string. I prefer to not pass it directly in function

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

As you have resolved display list of articles issue so I am referring update view issue here and skipping one view here. If you need any clearification on that too, then your most welcome.

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

Here I have explained in detail. Please let me know if you need any help.