3

I have an HTML table full of inputs populated by data in a database table. I generate a form for each row as a separate table, and a submit button for each. I want the update button to update only the row associated with the generated form.

If I remove this->db->where('id', $id); from the model, it updates all rows in the table with the same info. With the where() call present, nothing is updated.

VIEW

<?php foreach($log->result() as $row): ?>
<?=form_open('hq/update_entry');?>
    <table class="editLog">
        <tr>
            <td><?php 
                $data = array(
                    'name' => 'no',
                    'value' => $row->no
                );
                echo form_input($data);
                ?></td>
            <td><?php 
                $data = array(
                    'name' => 'date',
                    'value' => $row->date
                );
                echo form_input($data);
                ?></td>
            <td><?php 
                $data = array(
                    'name' => 'artist',
                    'value' => $row->artist
                );
                echo form_input($data);
                ?></td>
            <td><?php 
                $data = array(
                    'name' => 'title',
                    'value' => $row->title
                );
                echo form_input($data);
                ?></td>
            <td><?php 
                $data = array(
                    'name' => 'long',
                    'value' => $row->long
                );
                echo form_input($data);
                ?></td>
            <td><?php 
                $data = array(
                    'name' => 'lat',
                    'value' => $row->lat
                );
                echo form_input($data);
                ?></td>
                <td>
            <?php
                echo form_hidden('id', $row->id);
                $data = array(
                    'class' => 'updateSubmit',
                    'value' => '✚'
                );
                echo form_submit($data);
            ?></td>
        </tr>
    </table>
<?=form_close();?>

CONTROLLER

public function update_entry()
{
    $this->load->model('update_entry_model');
    
    if ($query = $this->update_entry_model->update()) {
        $this->index();
    } else {
        redirect('hq');
    }
}

MODEL

class Update_entry_model extends CI_Model
{
    public function update()
    {
        $data = array(
            'no' => $this->input->post('no'),
            'date' => $this->input->post('date'),
            'artist' => $this->input->post('artist'),
            'title' => $this->input->post('title'),
            'long' => $this->input->post('long'),
            'lat' => $this->input->post('lat')
        );
        $this->db->where('id', $id);
        $this->db->update('entries', $data);
    }
}
1
  • Model methods should not be directly accessing submission payloads or session data because this limits method utility and makes unit testing more complicated than it needs to be. Commented Aug 26 at 22:50

1 Answer 1

3

Without doing further investigation, I see that in the model function update(), you are missing

$id = $this->input->post('id');

The reason why adding this->db->where('id', $id); doesn't update anything is because $id is not currently set to any value.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.