So I’m new to PHP and MySQL generally, and MVC applications. Forgive me if this is a question that has been answered already, I couldn’t find anything that was crystal clear and showed the complete MVC flow of this.
So I have an HTML table full of inputs populated by data in a, 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 remove this->db->where('id', $id); from the model it updates all rows in the table with the same info, with it present nothing is updated.
Any help or an alternative method for achieving this would be most appreciated. The controller function is also not pretty bad I think.
Thank you.
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
function update_entry()
{
$this->load->model('update_entry_model');
if($query = $this->update_entry_model->update())
{
$this->index();
}
else
{
redirect('hq');
}
}
MODEL
<?php
class Update_entry_model extends CI_Model {
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);
}
}
Without doing further investigation, I see that in the model function update(), you are missing
The reason why adding
this->db->where('id', $id);doesn’t update anything is because $id is not currently set to any value.