Hi i have been trying to update a specific record on my database. After getting it's id that's passed in url using "baseurl" so far I can pass the variable but cannot seem to update the record. Here's my controller class update_request_record.php which holds the function Update function that is used to fetch the entire record from the database, which the loads the Update_request_view_page.php which holds the form that will enable a user to update the required fields. so far I have encountered the problem of the form not displaying at all, please help.Thanks
<?php
class Update_Request_Record extends CI_Controller {
public function Update_Request_Record()
{
parent::__construct();
}
function Update()
{
$id = $this->uri->segment(3);
$this->load->database();
$query = $this->db->get_where('test_request', array('id' => $id));
return $query->result();
$this->load->view('update_request_view'.$query);
}
function Submit()
{
$this->load->model('update_requestmodel');
if($this->input->post('submit')){
$this->update_requestmodel->Update();
}
//redirect('home');
}
}
?>
Here's my Model
<?php
class Update_Requestmodel extends CI_Model {
// model constructor function
function __construct() {
parent::__construct(); // call parent constructor
$this->load->database();
}
function Update()
{
$new_data = array(
'applicant_name'=>$this->input->post('applicant_name'),
'applicant_address'=>$this->input->post('applicant_address')
);
$this->db->update('test_request', $new_data);
//redirect('home');
}
}
?>
This is from my list view page that passes the id of a record to the controller Update_request_record.php
<table class="list_header" bgcolor="#ffffff" border="0" width="960px" cellpadding="4px">
<?php foreach($query as $row): ?>
<tr>
<td style="border-right: dashed 1px #c0c0c0;text-align: center;border-bottom: solid 1px #c0c0c0;"><?php echo $row->id; ?>.</td>
<td style="text-align: left;border-bottom: solid 1px #c0c0c0;" ><?php echo $row->testing_reason;?></td>
<td style="text-align: left;border-bottom: solid 1px #c0c0c0;"><?php echo $row->applicant_name;?></td>
<td style="text-align: left;border-bottom: solid 1px #c0c0c0;"><?php echo $row->authorizer_name;?></td>
<td style="text-align: left;border-bottom: solid 1px #c0c0c0;"><?php echo $row->received_by;?></td>
<td style="text-align: left;border-bottom: solid 1px #c0c0c0;"><?php echo $row->test_required;?></td>
<td style="text-align: left;border-bottom: solid 1px #c0c0c0;"><?php echo $row->test_specification;?></td>
<td style="text-align: left;border-bottom: solid 1px #c0c0c0;"><?php echo $row->laboratory_number;?></td>
<td style="text-align: left;border-bottom: solid 1px #c0c0c0;"><?php echo $row->date;?></td>
<td style="text-align: left;border-bottom: solid 1px #c0c0c0;"><a href="<?php echo base_url().'update_request_record/Edit/'.$row->id;?>">Edit</a></td>
</tr>
<?php endforeach; ?>
</table>
Here's my form update_request_view.php
<div id="analysis_request" class="analysis_request" >
<?php echo validation_errors(); ?>
<?php echo form_open('update_request_record/Submit',array('name'=>'test_request_update_form'));?>
<table class="table_form" width="750px" height="500px" border="0" cellpadding="4px" align="center">
<tr>
<td colspan="7" align="center" style="padding:8px;border-bottom: solid 1px #bfbfbf;">
<b><h3>UPDATING TEST REQUEST FORM</h3></b>
</td>
</tr>
<tr>
<td align="center" style="border-right: dashed 1px #bfbfbf;"><b>1.</b></td>
<td colspan="2"><b>Name of Applicant</b></td>
<td colspan="4"><textarea rows="1" cols="50" name="applicant_name" value="<?php echo $query->applicant_name;?>"></textarea></td>
</tr>
<tr>
<td align="center" style="border-right: dashed 1px #bfbfbf;"></td>
<td colspan="2"><b>Address of Applicant</b></td>
<td colspan="4"><textarea rows="1" cols="50" name="applicant_address"></textarea></td>
</tr>
<tr>
<td style="border-top: dotted 1px #bfbfbf;text-align: center;" colspan="7" ><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
</div>
</div>
Post your $id
from your form in an hidden input
named id
here, Then try this:
Add this line inside your form:
<?php echo form_hidden('id', $query->id) ?>
Then in your model do the following change:
$this->db->where('id', $this->input->post('id'));
$this->db->update('test_request', $new_data);
Just as a suggested improvement to your code to make it slightly more DRY, you could throw this into your model:
$column_mapping = array(
'db_col1' => 'input_field1'
//more columns/fields
);
foreach ($column_mapping as $k => $v) {
$new_data[$k] = $this->input->post($v);
}
Personally, I see a whole bunch of repeated code such as 'something' => $this->input->post('something');
and think "AHHHH!".