I have a problem when I want to insert multiple fields which are using foreach loop (to get the value form database), here is my form:
<form class="stdform" action="<?php echo $action_url; ?>" method="post">
<label>Check Date</label> <input class="span3" type="text"
id="datepicker" name="check_date" value="" required="required"
placeholder="Tanggal cek" />
<table class="table table-condensed table-hover table-bordered"
style="font-size: 12;">
<thead>
<tr>
<th class="head0">#</th>
<th class="head0">Device</th>
<th class="head0">IP Address</th>
<th class="head0">NAT IP Address</th>
<th class="head0">Check OS</th>
<th class="head0">Application/Server</th>
<th class="head0">Shift A</th>
<th class="head0">Shift B</th>
<th class="head0">Remark</th>
<th class="head0">Report</th>
</tr>
</thead>
<tbody>
<!-- Row Template -->
<?php
$num=1;
foreach( $get_server_assets as $row ) {
?>
<tr>
<td>
<?php echo $num; ?>
</td>
<td><input class="span12" type="text" name="device[]"
value="<?php echo $row->s_device; ?>" /></td>
<td><input class="span9" type="text" name="ip_address[]"
value="<?php echo $row->s_ip; ?>" /></td>
<td><input class="span9" type="text" name="nat_ip[]"
value="<?php echo $row->s_nat_ip; ?>" /></td>
<td><select class="span12" name="check_os[]">
<option value=""></option>
<option value="Good">Good</option>
<option value="Not Good">Not Good</option>
</select></td>
<td><input class="span10" type="text" name="app_name[]"
value="<?php echo $row->app_server; ?>" /></td>
<td><select class="span12" name="check_app_a[]">
<option value=""></option>
<option value="Good">Good</option>
<option value="Not Good">Not Good</option>
</select></td>
<td><select class="span12" name="check_app_b[]">
<option value=""></option>
<option value="Good">Good</option>
<option value="Not Good">Not Good</option>
</select></td>
<td><input class="span12" type="text" name="remark[]" value="" /></td>
<td><input class="span12" type="text" name="report[]" value="" /></td>
</tr>
<?php $num++; } ?>
</tbody>
</table>
<p class="stdformbutton">
<button class="btn btn-primary btn-large" type="submit" name="submit"
value="true">Submit</button>
<a class="btn btn-large" onclick="window.history.back()">Cancel</a>
</p>
</form>
And here is my controller:
$data = array();
$count = count($this->input->post('device'));
for($i=0; $i<$count; $i++) {
$data[] = array(
'check_date'=>$this->input->post('check_date'),
'device'=>$this->input->post('device' . $i),
'ip_address'=>$this->input->post('ip_address' . $i),
'nat_ip'=>$this->input->post('nat_ip' . $i),
'check_os'=>$this->input->post('check_os' . $i),
'app_name'=>$this->input->post('app_name' . $i),
'check_app_a'=>$this->input->post('check_app_a' . $i),
'check_app_b'=>$this->input->post('check_app_b' . $i),
'remark'=>$this->input->post('remark' . $i),
'report'=>$this->input->post('report' . $i),
);
}
$this->db->insert_batch('server_checklist',$data);
/*foreach($_POST['data'] as $d) {
$this->db->insert('server_checklist',$d);
} */
redirect('admin/viewServerChecklist');
When I submit there is no data insert, only check_date stored. And the rest give value 0.
Can someone help me??
try this, instead of appending, try to access the index of the array.
$data[] = array(
'check_date'=>$this->input->post('check_date'),
'device'=>$this->input->post('device')[$i],
'ip_address'=>$this->input->post('ip_address')[$i],
'nat_ip'=>$this->input->post('nat_ip')[$i],
'check_os'=>$this->input->post('check_os')[$i],
'app_name'=>$this->input->post('app_name')[$i],
'check_app_a'=>$this->input->post('check_app_a')[$i],
'check_app_b'=>$this->input->post('check_app_b')[$i],
'remark'=>$this->input->post('remark' )[$i],
'report'=>$this->input->post('report')[$i],
);