Why does CI create a new row instead of updating? Why does CI create a new row instead of updating? codeigniter codeigniter

Why does CI create a new row instead of updating?


There are a number of mistakes here.

SO to date we have established that performing var_dumps in the controller results in NULL for all your "POST" values.

I've assumed the following for simplicity.

Controller Name is: Program.php (Application is NOT an Allowed controller name as it's a foldername)

Model Name is: Mdl_update.php

View is: update_view.php

Issue #1:Your Form has an issue where you are using an anchor tag which is just a link. It does nothing in submitting any data from the form.

So we have to remove the Anchor Tag and replace it with a Form Submit. You have to Submit the form to get any chance of sending the form data.

For testing your GET and POST I've added in Two different Forms.In update_view.php

<!-- Set the Method to GET --><form action="program/update" method="get">    <input type="hidden" name="mar-id" value="<?php echo $row['id']; ?>">    <input type="hidden" name="mar-read" value="New-value">    <input type = "submit" name="update" value="Update with GET"></form><!-- Set the Method to POST as this is what the Controller is Expecting --><form action="program/update" method="post">    <input type="hidden" name="mar-id" value="<?php echo $row['id']; ?>">    <input type="hidden" name="mar-read" value="New-value">    <input type = "submit" name="update" value="Update with POST"></form>

What I used to display the Form in the controller by simply calling the program/index in the Program controller.

public function index() {    $this->load->helper('url');    $data['row'] = array('id' => 2);    $data = $this->load->view('update_view', $data, TRUE);    echo $data;}

So your Controller is looking for POST and not GET. This can be proven by changing the controller up a bit for debugging.

public function update() {    $this->load->helper('url');    $this->load->model('mdl_update');    $id = $this->input->post('mar-id');    $value = $this->input->post('mar-read');    echo '<h2>POST Values</h2>';    var_dump($id);    var_dump($value);    // ****************************    // These are added in for debugging/Demonstration to show values for the form using the GET method.    $id_get = $this->input->get('mar-id');    $value_get = $this->input->get('mar-read');    echo '<h2>GET Values</h2>';    var_dump($id_get);    var_dump($value_get);    // ****************************    exit('Stopped for Debugging: Method '. __METHOD__.' at Line: '.__LINE__); // Added for Debug    $mar = $this->mdl_update->Update($id, $value);    if ($mar == TRUE) {        redirect(base_url('program/otherpage'), 'refresh');    } else {        redirect(base_url('program/otherpage'));    }}

So you are looking for POST Data when your form method is set to GET. Please be aware of what you are setting. They must match.

If you want to use GET, you need to use $this->input->get()The code above will let you test both.

So you now have a POST and GET Form and the controller is setup to demonstrate the two different types. Choose Either GET or POST!. That is up to you on which one you choose.

Issue #2: Expecting a return value from your Model when you are not returning anything.

In your Controller you have the line...

$mar = $this->mdl_update->Update($id, $value);

And in your Model you have...

public function update ($id,$value) {    $data = array(        'status' => $value    );    $this->db->where('id', $id);    $this->db->update('db_table', $data);}

Your Model Method is not returning anything.You should always look up what your return values are. I am expecting that your intention was to return the value of the update. Looking through the CI Code itself it appears that if things go wrong it will return FALSE (if the database debug is disabled - learnt something new)

I've added in some debug to assist in viewing what is going on here.

public function update($id, $value) {    $data = array(        'status' => $value    );    $this->db->where('id', $id);    $update_result = $this->db->update('db_table', $data);    echo $this->db->last_query(); // Added for DEBUG    return $update_result;}

Now I cannot get your code to create new rows as you claim. It's impossible, with this code, to add new rows. So thats happening from something you haven't shown us but that is an aside and not important here.

If we alter the controller to view the model etc (I am only showing the changes ) we would change

exit('Stopped for Debugging: Method '. __METHOD__.' at Line: '.__LINE__);$mar = $this->mdl_update->Update($id, $value);

To this

$mar = $this->mdl_update->Update($id, $value);var_dump($mar);exit('Stopped for Debugging: Method '. __METHOD__.' at Line: '.__LINE__);

If you run this and submit either the GET ( Results are NULL ) or POST, the update will always return TRUE. So your redirect etc needs to be looked at on how you decide on one or the other.

I think you should set your table columns to not allow them to be NULL AND add in some "Validation" in your controller.

ISSUE 3: No Form ValidationCodeIgniter has a Form Validation Class that I suggest you read. This is getting way too long to go into that here...

So as you go through this, you can add/remove debugging to test what is going on and progress it along the way as I have hopefully shown.

if anything is unclear, just ask. I'm sure I may have left something out.