shank shank - 3 months ago 19
MySQL Question

Codeigniter : Multiple columns of the same row Update query not working

I am not able to update multiple columns of the same row with the row matching the userid in codeigniter.

My controller Code is :

exits.php



function update_act_on_resignation(){
global $SITE,$USER;
$data = array();
$data['row'] = new stdClass();
$data['row'] = $this->admin_init_elements->set_post_vals($this->input->post());
$data['offices']=$this->mod_common->get_all_offices();
$clients = currentuserclients();
$data['roles'] = $this->mod_common->get_cat_array('designation','status',"1' AND id > '0",'designation');
get_city_state_country_array($data,array('cityid'=>$data['row']->cityid));
$data['error_message'] = '';
$data['row']->id = $this->uri->segment(3);
$data['id'] = $this->uri->segment(3);
$data['action'] = 'add';
$data['heading'] = 'Add';
$data['msg_class'] = 'sukses';
$data['path']=$path;
$post_action = $this->input->post('action');
if($post_action=='add' || $post_action =='update' ){
$post_array = $this->input->post();
$action = ($post_action == 'add')?'inserted':'updated';
//echo '<pre>';print_r($SITE);die;
echo $post_array['exit_type'] = 'Employee Initiated';
echo $post_array[$id] = $USER->id;
echo $post_array['custom-105965'];
echo $post_array['manager_comments'];
echo $post_array['accept'];
echo $post_array['agreed_last_date'];


$data['success_message'] = $this->exit_common->update_get_resignation_to_act($post_array,$action);


if($data['success_message'] == 'Record '.$action.' successfully'){
$data['row'] = new stdClass();
$data['row']->id = $this->uri->segment(3);
$data['row']->status = 1;

}

}

$this->data['maincontent'] = $this->load->view('maincontents/view_resignation_action', $data,true);
echo "Resignation withdrawn successfully!";

$this->load->view('layout', $this->data);
}





I am echoing the results got from a form to check if i am receiving the sent form elements and i have received everything properly. i.e i am recieving 67 for $post_array[$id] Yes for $post_array['custom-105965'] asd for $post_array['manager_comments'] Accept for $post_array['accept'] and 01-01-1970 as $post_array['agreed_last_date'] after the echo.

My Model code is:

exit_common.php



function update_get_resignation_to_act($post_array,$action){

$this->load->database();
$this->db->where('userids', $post_array['id']);
$dbdata['discussion'] = $post_array['custom-105965'];
$dbdata['manager_comments'] = $post_array['manager_comments'];
$dbdata['last_status'] = $post_array['accept'];
$dbdata['agreed_date'] = $post_array['agreed_last_date'];

$this->db->update('pr_resignation_requests', $dbdata);
return;



}





I think something is wrong with the model update function. But not able to figure out what is wrong as a similar function is working for updating a single column in db. Am i missing something? Its a mysql db.

My View Code is:

view_resignation_action.php



<style>
label{font-weight:bold;}

.hbox .col {
display: table-cell;
float: none;
height: 100%;
vertical-align: top;
width:100%;
}
</style>
<div class="new">
<section class="content-header">
<h1>
Resignation Action
</h1>
<ol class="breadcrumb" >
<li><a href="<?php base_url(); ?>"><i class="fa fa-home"></i> Home</a></li>
<li><a href="#">People Connect</a></li>
<li><a href="<?php echo base_url(); ?>exits">Exit Tracker</a></li>
<li class="active">Exit Details</li>

</ol>
</section>
<input type="hidden" id="page_name" value="requests">

<div class="container-fluid">
<div class="row">
<!-- Thought Day-->
<div class="panel wrapper clearfix m-b-none">
<!-- Horizontal Form -->

<div class="box-header with-border">

</div><!-- /.box-header -->
<!-- form start -->
<?php if($error_message!=''){?>
<div class="row-fluid return-message success-message <?php echo $msg_class; ?>"><?php echo $error_message;?></div>
<?php } else { ?>
<?php echo form_open('exits/update_act_on_resignation/',array('name'=>'addostcstevent','id'=>'addostcstevent','method'=>'post','autocomplete'=>'on','class'=>'form-horizontal'))?>
<?php echo form_hidden(array('id'=>$USER->id,'action'=>$action));?>

<?php foreach($rows as $row){ ?>
<div class="panel-body">
<div class="control-group form-group">
<div class="col-md-6">

<div class="controls">
<div class="col-md-5">

<label class="control-label" style="text-align:left">Name of the employee </label>
<div class="controls"><?php echo $row->firstname; ?>
</div>

</div>
<div class="col-md-5">

</div>
</div>
</div>
<div class="col-md-6 pull-right">
<label class="control-label" style="text-align:left">Date of request </label><div class="controls">
<?php echo $row->resignations_date; ?>
</div>
</div>
</div>
<div class="clearfix"></div>
<div class="control-group form-group">
<div class="col-md-6 ">
<label class="control-label" style="text-align:left">Requested Last Working Date</label>
<div class="controls">
<div class="col-md-10">
<?php echo $row->requested_date; ?>
</div>
</div>
</div>

<div class="col-md-6 pull-right">
<label class="control-label">Reason</label>
<div class="controls">
<div class="col-md-9">
<?php echo $row->exit_type; ?>
</div>
</div>
</div>

<div class="col-md-6 pull-right">
<label class="control-label">Comments from Employee</label>
<div class="controls">
<div class="col-md-9">
<?php echo $row->comments; ?>
</div>
</div>
</div>



<br><br><br><br><br><br><br>


<fieldset id="f1" class="col-md-6 pull-left"> <!-- start fieldset -->


<div class="label">



</label>
</div>
<div class="input">

</div>
</div>
</td>
</table>
</td>
</tr>
<tr>
<td>
<table width="100%" style="text-align:left">

<label class="control-label" style="text-align:left">Have you had a discussion with Employee?</label>
<table>
<td valign="top">
<input id="custom-105965_0" class="custom-105965" name="custom-105965" value="Yes" type="radio">
<label for="custom-105965_0">Yes
<br>
</label>
</td>
<td valign="top">
<input id="custom-105965_1" class="custom-105965" name="custom-105965" value="No" type="radio">
<label for="custom-105965_1">No

</label>
</td>
</table>
<td valign="top">
<div class="fieldset" id="bsd-field-custom-105967-group">
<div class="col-md-6 pull-left">
<label class="control-label">Please enter your comments</label>
<div class="controls">
<div class="col-md-12">
<textarea required class="form-control" name="manager_comments"></textarea>
<!--<input placeholder="Please enter your comments" class="" id="dateofrequest" type="text" name="todays_date" value=""/> <br><br>-->
</div>
</div>

<label class="control-label">Action</label>
<div class="controls">
<div class="col-md-9">
<?php $dd_list = array(
'Accept' => 'Accepted',
'Reject' => 'Rejected',
);
echo form_dropdown('accept', $dd_list, 'Accept');
?>
</div>
</div>





<label class="control-label">Agreed Last Working day</label>
<div class="controls">
<div class="col-md-9">
<input placeholder="Agreed Last Working day" class=" m-wrap col-md-8 form-control " id="startdt" type="text" name="agreed_last_date" value="<?php if($row->requested_date!='') echo date("d-m-Y",$row->requested_date); ?>" required/>
</div>
</div>

<br><br>

<div class="col-md-9">


</div>




</div>



</div>
<!-- If its a no -->
<div class="fieldset" id="bsd-field-custom-105867-group">
<div class="col-md-6 pull-left">


<div class="alert alert-success alert-dismissable">
<label type="button"></label>
Please have a discussion with the employee.
</div>



</div>



</div>
</td>
</table>
</td>
</tr>








































</fieldset><!-- End Fiedset -->

</div>
</div>
<?php }?>
<div class="clearfix"></div>
<div class=" box-footer">
<button type="submit" class="btn btn-info pull-right" style="margin:0px 10px">submit</button>

</div>
</form>
<?php } ?>
<div class="clearfix"></div>
</div>
</div>
</div>
</section>
</div>
<script type="text/javascript" src="<?php echo base_url(); ?>assets/plugins/bootstrap-fileupload/bootstrap-fileupload.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>assets/plugins/select2/select2.min.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>assets/plugins/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>assets/plugins/bootstrap-timepicker/js/bootstrap-timepicker.js"></script>

<script>
$(".fieldset").hide();
$(document).ready(function () {
$("[id^=bsd-field-custom-1059]").hide();
$(".custom-105965").change(function () {
$("[id^=bsd-field-custom-1059]").toggle(this.value == 'Yes');
});

});
$(document).ready(function () {
$("[id^=bsd-field-custom-1059]").hide();
$(".custom-105965").change(function () {
$("[id^=bsd-field-custom-1058]").toggle(this.value == 'No');
$('.alert').show()
});

});
$(document).ready(function(){
$("#startdt").datepicker({
dateFormat: 'dd-mm-yy',
changeMonth: true,
changeYear: true,
showButtonPanel: true,
yearRange: "-90:+0",
endDate: new Date(),
autoclose: true
}).on('changeDate', function (selected) {
var minDate = new Date(selected.date.valueOf());
$('#enddt').datepicker('setStartDate', minDate);
});
$("#enddt").datepicker({
dateFormat: 'dd-mm-yy',
changeMonth: true,
changeYear: true,
showButtonPanel: true,
yearRange: "-90:+0",
endDate: new Date()
});
$(".date-picker").datepicker();
$('.timepicker-default').timepicker();
$('#report_date').datepicker( {
format: "mm-yyyy",
viewMode: "months",
minViewMode: "months"
});

$(".select2").select2({
placeholder: "Select an option",
allowClear: true
});
$("#asset_type").change(function(){
if($(this).val() == '-1') {
$("#new_type").show();
}
else
{
$("#new_type").hide();
}
});

$("#manufacturer").change(function(){
if($(this).val() == '-1') {
$("#new_manufacturer").show();
}
else
{
$("#new_manufacturer").hide();
}
});

$("#supplier").change(function(){
if($(this).val() == '-1') {
$("#new_supplier").show();
}
else
{
$("#new_supplier").hide();
}
});
});



</script>





Mysql Table is :enter image description here

Answer

Try Below Snippet

function update_get_resignation_to_act($post_array,$action)
{       
     $this->load->database(); 
     $this->db->where('userids', $post_array['id']); 
     $dbdata = array(
          "discussion" => $post_array['custom-105965'],
          "manager_comments" => $post_array['manager_comments'],
          "last_status" => $post_array['accept'],
          "agreed_date" => $post_array['agreed_last_date']
     ); 
     $this->db->update('pr_resignation_requests', $dbdata);
     /**
     * if required add this code here to check
     *
     * echo $this->db->last_query();
     */
     return;    
}