Welcome Guest, Not a member yet? Register   Sign In
updating a record
#1

[eluser]cerberus478[/eluser]
Hi I'm using codeigniter 2.1.4.
I'm trying to update an existing record. When I update a record all the records end up having the same value as the record I updated.

My pages controller
Code:
<?php

class Pages extends MX_Controller {

function __construct() {
  parent::__construct();
}

function manage(){
  
  $data = array();
  $this->load->model('pages_model');
  
  if($query = $this->pages_model->get_pages()){
   $data['pages'] = $query;
  }
  //$data['pages'] = $this->pages_model->get_pages();

  $data['main_content'] = 'manage';
  $this->load->view('includes/template', $data);  
}


function create(){

  $this->load->helper('form');
  $this->load->library('form_validation');

  //$data['title'] = 'Create a news item';

  $this->form_validation->set_rules('title', 'Title', 'required');
  $this->form_validation->set_rules('content', 'Content', 'required');

  if ($this->form_validation->run() === FALSE) {
   $data['main_content'] = 'create';
   $this->load->view('includes/template', $data);
  }
else
  {
  $this->load->model('pages_model');
  $this->pages_model->set_pages();
  $this->load->view('pages/success');
  }
}

public function view($slug)
{
  $this->load->model('pages_model');
  $data['pages'] = $this->pages_model->get_pages($slug);
}
        
        public function delete() {
            $this->load->model('pages_model');
            $this->pages_model->delete_row();
            $this->manage();
        }

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

  //$data['title'] = 'Create a news item';

  $this->form_validation->set_rules('title', 'Title', 'required');
  $this->form_validation->set_rules('content', 'Content', 'required');

  if ($this->form_validation->run() === FALSE) {
   $data['main_content'] = 'edit';
   $this->load->view('includes/template', $data);
  }
else
  {
  $this->load->model('pages_model');
  $this->pages_model->update_record();
  $this->load->view('pages/success');
  }
        }
}

My pages_model
Code:
function update(){
           $this->load->helper('form');
  $this->load->library('form_validation');

  //$data['title'] = 'Create a news item';

  $this->form_validation->set_rules('title', 'Title', 'required');
  $this->form_validation->set_rules('content', 'Content', 'required');

  if ($this->form_validation->run() === FALSE) {
   $data['main_content'] = 'edit';
   $this->load->view('includes/template', $data);
  }
else
  {
  $this->load->model('pages_model');
  $this->pages_model->update_record();
  $this->load->view('pages/success');
  }
        }

This is my edit.php view
Code:
<h2>Edit page</h2>

&lt;?php echo validation_errors(); ?&gt;

&lt;?php echo form_open('pages/update/'); ?&gt;

<label for="title">Title</label>
&lt;input type="input" name="title" /&gt;&lt;br />

<label for="content">Content</label>
&lt;textarea name="content"&gt;&lt;/textarea><br />

&lt;input type="submit" name="submit" value="Create news item" /&gt;

&lt;/form&gt;
#2

[eluser]Tpojka[/eluser]
Seems that controller method {update()} and model method {update()} have same code.
#3

[eluser]cerberus478[/eluser]
I didn't even notice that.

I tried adding this code to my pages controller
Code:
function update(){
            $data = array(
                'title' => $this->input->post('title'),
                'content' => $this->input->post('content')
            );
            
            $this->load->model('pages_model');
            $this->pages_model->update_record($data);
            
            $data['main_content'] = 'edit';
  $this->load->view('includes/template', $data);
        }

and this to my pages_model
Code:
public function update_record($data){
  $this->db->where('id', $this->uri->segment(3));
  $this->db->update('pages', $data);
}

but then one of the records ends up having an id of 0 and the record that I'm trying to update has the title of 0
#4

[eluser]Massaki[/eluser]
Your form (en edit.php) does not indicate the record ID you want to update.
You should send it to your 'view' inside the array $data.

Then, to send it when the form is submitted:
Code:
echo form_open('pages/update/' . $record_id);

Furthermore, the right way to create an input field is:
Code:
&lt;input type="text" name="title" /&gt;


Tip: When you are having a problem involving DB, try this:
Code:
echo $this->db->last_query();



#5

[eluser]cerberus478[/eluser]
Then to get it to work I should have this

pages controller
Code:
function update(){
            $data = array(
                'title' => $this->input->post('title'),
                'content' => $this->input->post('content')
            );

            $data['record_id'] = $this->input->post('id');
            
            $this->load->model('pages_model');
            $this->pages_model->update_record($data);
            
            $data['main_content'] = 'edit';
  $this->load->view('includes/template', $data);
        }

and the edit view
Code:
<h2>Edit page</h2>

&lt;?php echo validation_errors(); ?&gt;

&lt;?php  echo form_open('pages/update/' . $record_id);   ?&gt;

<label for="title">Title</label>
&lt;input type="text" name="title" /&gt;

<label for="content">Content</label>
&lt;textarea name="content"&gt;&lt;/textarea><br />

&lt;input type="submit" name="submit" value="Create news item" /&gt;

&lt;/form&gt;
#6

[eluser]Massaki[/eluser]
Well, if you want to retrieve the record ID from $_POST, then you should send it as a hidden variable.

Code:
echo form_open('pages/update', array('id'=>$record_id));


And modify pages_model:
Code:
public function update_record($data){
  $this->db->where('id', $data['record_id']);
  $this->db->update('pages', $data);
}





Theme © iAndrew 2016 - Forum software by © MyBB