简体   繁体   中英

How to update DB record using CodeIgniter?

I'm trying to update a record of a database.

Here is the code:

On my index page.

<?php foreach ($news as $news_item): ?>

<h2><?php echo $news_item['title'] ?></h2>
<div class="main">
    <?php echo $news_item['text'] ?>
</div>
<p><a href="news/<?php echo $news_item['slug'] ?>">View article</a></p>
<p><a href="news/delete/<?php echo $news_item['slug'] ?>">Delete article</a></p>
<p><a href="news/update/<?php echo $news_item['slug'] ?>">Update article</a></p>

On my update.php page.

<h2>Update a news item</h2>

<?php echo validation_errors(); ?>

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

<label for="title">Title</label>
<input type="input" name="title" /><br />

<label for="text">Text</label>
<textarea name="text"></textarea><br />

<input type="submit" name="submit" value="Update news item" />

</form>

On my news.php page.

    public function update($slug)
{

    $this->load->helper('url');
    $this->news_model->update_news($slug);
    redirect ('news', 'refresh');
}

On my news_model.php page.

    public function update_news($slug)
{

$this->load->helper('url');

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

$this->db->where('title', $slug);
$this->db->update('news', $data); 

}

My routing page.

$route['news/update/(:any)'] = 'news/update/$1';    
$route['news/delete/(:any)'] = 'news/delete/$1';
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

I'm trying to update the record the user clicks on.

However, instead of sending the slug to the update page, which has the form on to update the record, the code runs all the way through and updates the record to "0" values.

EDIT:

After a few comments, I can now see your problem. This is how I would fix it;

Your index file seems fine. It's your update function, within your controller where the problem lies. You're expecting to see the update form, but you're not actually loading the view;

Try replace the function, with this one;

public function update($slug)
{
    if (!$this->input->post('submit'))
    {
      // The form was NOT posted
      // So we load the view
      $data['selected_article'] = $this->news_model->get_by_slug($slug);
      $this->load->view('update', $data);
    }
    else
    {
      // The form was submitted
      $data = array(
        'title' => $this->input->post('title'),
        'slug' => $this->input->post('slug'),
        'text' => $this->input->post('text'),
      );

      $this->news_model->update_news($slug, $data);
    }
  }

Shouldn't this:

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

be this:

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

Also, are you sure that the $title variable is exactly the same as $slug ? If $title = 'My title', and $slug = 'my-title', they won't match and this won't run as expected:

$this->db->where('title', $slug);

I added a function in my form model file, there I just want to update some of the records that previously have been uploaded in a db:

public function update_records($id_aUsar){
    $data = array(
        'asunto'  =>  $this->input->post('tema'),
        'remite'  =>  $this->input->post('emite'),
        'mensaje' =>  $this->input->post('content'),
        'dirigido' => $this->input->post('paraGrupo'),
        'fechaEnvio'=> $this->input->post('fechaEnvio'),
        'observaciones' => $this->input->post('notas')
    );

    $this->db->where('id_masivos', $id_aUsar);
    $this->db->set('fecha', 'NOW()', FALSE);
    $this->db->update('masivos_texto', $data); 
}

where $id_aUsar is the primary key id of the row I want to update, I get the id from my session value, in order to keep the same session (my primary key id_masivos is type AUTOINCREMENT), the array entries are the only data to be updated after the user updates some of the fields (all of then including), but only those I show in the function are essential to the records update. Then in my controller I access my model added fuction like this:

$this->forma_model->update_records($id_tarea);

where $id_tarea is the current session id for that user in that session, that´s how I update records without losing the primary key id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM