我正在尝试将使用jquery创建的动态文本字段中的数据插入数据库中。但是,只有第一行数据存储在数据库中,而其他行则没有。我无法找到原因,因为我希望循环可以处理数组中的所有数据。我不知道我在做什么错。
<tbody id="item_body">
<tr id="item_row">
<th>1</th>
<td>
<select class="form-control" name="item_name[]" id="item_name" style=" width:150px;">
<option value="none" selected="" disabled="">Select Item</option>
<?php foreach ($items as $item):?>
<option value="<?php echo $item['id'];?>"><?php echo $item['item_name']; ?> </option>
<?php endforeach;?>
</select>
</td>
<td><input type="text" name="price_p_u[]" id="price_p_u" class="form-control"></td>
<td><input type="text" name="qty_unit[]" id="qty_unit" class="form-control"></td>
<td>
<select name="unit-type[]" id="unit-type" class="form-control" style=" width:90px;">
<option value="none" selected="" disabled="">Choose</option>
<option value="Unit">Unit</option>
<option value="Sub-Unit">Sub-Unit</option>
</select>
</td>
<td><input type="text" name="qty[]" id="qty" class="form-control"></td>
<td><input type="text" name="total_price[]" id="total_price" class="form-control"></td>
<td><input type="button" name="" id="add_fields" class="btn btn-success" value="+"></td>
</tr>
</tbody>
<tbody id="total_part">
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>Total</td>
<td id="grand_total">GH0.00</td>
</tr>
</tbody>
$('td #add_fields').click(function(){
item_count++;
var url = baseURL+'interfaces/population';
$.ajax({
type: "GET",
url: url,
data:'',
dataType: 'json',
success: function(res){
$('#item_body').append('<tr id="item_row_add'+item_count+'">'+
'<th>'+item_count+'</th>'+
'<td>'+
'<select class="form-control" name="item_name[]" id="item_name'+item_count+'" style=" width:150px;">'+
'<option value="none" selected="" disabled="">Select Item</option>');
for(i in res){
$('#item_row_add'+item_count+' select').append('<option value="'+res[i].id+'">'+res[i].item_name+'</option>');}
// alert(res[i].item_name);
$('#item_row_add'+item_count+'').append('</select>'+
'</td>'+
'<td><input type="text" name="price_p_u[]" id="price_p_u'+item_count+'" class="form-control"></td>'+
'<td><input type="text" name="qty_unit[]" id="qty_unit'+item_count+'" class="form-control"></td>'+
'<td>'+
'<select name="unit-type[]" id="unit-type'+item_count+'" class="form-control" style=" width:90px;">'+
'<option value="none" selected="" disabled="">Choose</option>'+
'<option value="Unit">Unit</option>'+
'<option value="Sub-Unit">Sub-Unit</option>'+
'</select>'+
'</td>'+
'<td><input type="text" name="qty[]" id="qty'+item_count+'" class="form-control"></td>'+
'<td><input type="text" name="total_price[]" id="total_price'+item_count+'" class="form-control"></td>'+
'<td><input type="button" name="" class="btn btn-danger remove_fields" value="X" ></td>'+
'</tr>');
}
});});
public function new_sale(){
$data['title']='Add sale';
$this->form_validation->set_rules('sales_date', 'Date', 'required');
$this->form_validation->set_rules('name_customer', 'Name of Customer', 'required');
$data['id']=$this->input->post('item_name');
$data['items']=$this->interface_model->get_item_data();
$data['lastInvoiceNo']=$this->interface_model->check_invoice_no();
if($this->form_validation->run()==FALSE){
$this->load->view('templates/header_interfaces');
$this->load->view('interfaces/new_sale', $data);
$this->load->view('templates/footer_interfaces');
}else{
$this->interface_model->new_sale();
$this->session->set_flashdata('sales_registered','Sale Recorded');
redirect('interfaces/new_sale');
}}
public function new_sale(){
//$item_id=$this->input->post('item_name');
$sales_date=$this->input->post('sales_date');
$name_customer=$this->input->post('name_customer');
$invoice_no=$this->input->post('invoice_no');
$item_id=$this->input->post('item_name');
$unit_type=$this->input->post('unit-type');
$qty=$this->input->post('qty');
$r_total_price=$this->input->post('total_price');
for($i=0; $i<count($r_total_price); $i++){
$data=array('sales_date'=>$sales_date,
'name_customer'=>$name_customer,
'invoice_no'=>$invoice_no,
'item_id'=>$item_id[$i],
'unit-type'=>$unit_type[$i],
'qty'=>$qty[$i],
'r_total_price'=>$r_total_price[$i]);
return $this->db->insert('sales',$data);
}
}
答案 0 :(得分:2)
希望这对您有帮助:
在return
for
循环中删除$this->db->insert('sales',$data);
您的new_sale()
方法应如下所示:
public function new_sale()
{
$sales_date=$this->input->post('sales_date');
$name_customer=$this->input->post('name_customer');
$invoice_no=$this->input->post('invoice_no');
$item_id=$this->input->post('item_name');
$unit_type=$this->input->post('unit-type');
$qty=$this->input->post('qty');
$r_total_price=$this->input->post('total_price');
for($i=0; $i<count($r_total_price); $i++)
{
$data = array('sales_date'=>$sales_date,
'name_customer'=>$name_customer,
'invoice_no'=>$invoice_no,
'item_id'=>$item_id[$i],
'unit-type'=>$unit_type[$i],
'qty'=>$qty[$i],
'r_total_price'=>$r_total_price[$i]
);
$this->db->insert('sales',$data);
}
}