I have a table name ‘payroll’ in my DB having the fields salary, bonus, overtime fields for employees. The salary of an employee is saved in the table. What i’m trying to do is to calculate the bonus field. That means if salary of an employee is lets say 2000 and bonus is 500 then in the “bonus” field it will be saved as 2500. Problem is that my code does not show any error but the field “overtime” is not updating in DB.
I’m using codigniter
my controller:
function update_bonus(){
$id=$this->input->post('id');
$salary=$this->input->post('salary');
$bonus=$this->input->post('bonus');
$update=$salary+$bonus;
$data['bonus']=$this->input->post('update');
if ($this->pay->update_overtime($id,$data)==TRUE){
$this->load->view('success');
}
else
{
$this->load->view('unsuccess');
}
}
My model:
function update_overtime($id,$data){
$this->db->where('id', $id);
$this->db->update('payroll', $data);
if ($this->db->affected_rows() == '1')
{
return TRUE;
}
return FALSE;
}
}
The form:
echo form_open('payroll/update_bonus');?>
<p> Previous Salary was <? echo $info[0]->salary;?> .</p>
<p>
<?php echo form_hidden('id', $id, 'id="id'); ?>
</p>
<p>
<?php echo form_hidden('id', $salary, 'id="salary'); ?>
</p>
<label for="bonus">Update Basic Salary <span class="required">*</span></label>
<?php echo form_error('salary'); ?>
<br /><input id="bonus" type="text" name="bonus" value="<?php echo set_value('bonus'); ?>" />
</p>
<p>
<?php echo form_submit('submit', 'Submit'); ?>
</p>
<?php echo form_close(); ?>
You have got your variables and form element names mixed up –
In your for view – you have got these
hiddenelementsI guess you wanted them to be –
In your
controllerupdate_bonus()functionYou are tying to use ‘update’ value from post data which was never set in your view file.
I guess you you wanted
$update=$salary+$bonus;// I assume$data['bonus']=$update;
bonusis a database table column name and this is the only column you want to update in yourmodel