我想保存具有多行的表,当我尝试保存数据时,动态生成的第一个字母保存在数据库中。当我尝试保存下一行时,它在控制台中显示500错误。我想在数据库中逐个保存所有行的所有数据我该如何实现?
这是我的代码:
<!-- <form action="" method="POST"> -->
<?php $data = array(
'id'=>'inventory_form',
); ?>
<?php echo form_open('main/store',$data); ?>
<div class="table-responsive">
<table class="table table-bordered table-hover" id="inv_tbl">
<thead>
<th>No</th>
<th>~</th>
<th>Barcode</th>
<th>Product Name</th>
<th>SM</th>
<th>SPL</th>
<th>Quantity</th>
<th>Price</th>
<th>Discount</th>
<th>Amount</th>
<th><input type="button" value="+" id="add" class="btn btn-primary add_inv_row"></th>
</thead>
<tbody class="detail">
<tr>
<td class="no">1</td>
<td class="text-center"><input type="checkbox" id="till_check" class="is_checked" name="till_check[]" value="Bike"></td>
<td>
<select name="barcode[]" id="barcode" class="form-control selectpicker barcode" data-live-search="true">
<option value="">Please select a barcode</option>
<?php foreach($barcodes as $barcode) :?>
<option value="<?php echo $barcode->barcode; ?>"><?php echo $barcode->barcode; ?></option>
<?php endforeach; ?>
</select>
</td>
<td><input type="text" id="brcode_product" class="form-control productname" name="productname[]"></td>
<td>
<select name="sm[]" id="sm" class="form-control selectpicker sm" data-live-search="true">
<option value="">Please select a Employee code</option>
<?php foreach($employee_codes as $employee_code) :?>
<option value="<?php echo $employee_code->emp_code; ?>"><?php echo $employee_code->emp_code; ?></option>
<?php endforeach; ?>
</select>
</td>
<td><input type="text" id="spl" class="form-control spl" name="spl[]"></td>
<td><input type="text" id="quantity" class="form-control quantity" name="quantity[]"></td>
<td><input type="text" id="price" class="form-control price" name="price[]"></td>
<td><input type="text" id="discount" class="form-control discount" name="discount[]"></td>
<td><input type="text" id="amount" class="form-control amount" name="amount[]"></td>
<td><a href="#" class="remove">Delete</a></td>
</tr>
</tbody>
<tfoot>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th class="text-center"><button class="btn btn-success" id="save_btn" type="submit" name="sale_submit_btn">Save</button></th>
<th style="text-align:center;" class="total">0<b></b></th>
</tfoot>
</table>
</div>
<?php echo form_close(); ?>
<!--</form>-->
我的Javascript代码: -
function total() {
var t = 0;
$('.amount').each(function (i, e) {
var amt = $(this).val() - 0;
t += amt;
});
$('.total').html(t);
}
function addnewrow() {
var n = ($('.detail tr').length - 0) + 1;
var tr = '<tr>' +
'<td class="no">' + n + '</td>' +
'<td><input type="checkbox" class="till_check" name="till_check[' + till_check_counter + ']" id="till_check[' + till_check_counter + ']"></td>' +
'<td><select class="form-control barcode selectpicker dyselect_' + product_barcode_counter + '" data-live-search="true" name="barcode[' + product_barcode_counter + ']" id="barcode[' + product_barcode_counter + ']">' + '<option>Please select a bar code</option>' + '</select></td>' +
'<td><input type="text" class="form-control prdctn_' + product_name_counter + ' productname" id="brcode_product['+product_name_counter+']" name="productname[' + product_name_counter + ']" id="productname[' + product_name_counter + ']"></td>' +
'<td><select class="form-control selectpicker dysm_' + sm_counter + ' sm " data-live-search="true" name="sm[' + sm_counter + ']" id="sm[' + sm_counter + ']">' + '<option>Please select a Employee code</option>' + '</select></td>' +
'<td><input type="text" class="form-control spl" name="spl[' + spl_counter + ']" id="spl[' + spl_counter + ']"></td>' +
'<td><input type="text" class="form-control quantity" name="quantity[' + product_quantity_counter + ']" id="quantity[' + product_quantity_counter + ']"></td>' +
'<td><input type="text" class="form-control price" name="price[' + product_price_counter + ']" id="price[' + product_price_counter + ']"></td>' +
'<td><input type="text" class="form-control discount" name="discount[' + product_discount_counter + ']" id="discount[' + product_discount_counter + ']"></td>' +
'<td><input type="text" class="form-control amount" name="amount[' + product_amount_counter + ']" id="amount[' + product_amount_counter + ']"></td>' +
'<td><a href="#" class="remove">Delete</td>' +
'</tr>';
$('.detail').append(tr);
//increamenting the counter
++product_barcode_counter;
++till_check_counter;
++product_name_counter;
++product_quantity_counter;
++sm_counter;
++spl_counter;
++product_price_counter;
++product_discount_counter;
++product_amount_counter;
//setting the validation rules for every product attribute by calling the function
createValidation();
get_barcodes();
get_employee_codes();
}
$('#save_btn').on('click', function () {
var barcode = $('#barcode option:selected').val();
var productname = $('#brcode_product').val();
var sm = $('#sm option:selected').val();
var spl = $('#spl').val();
var quantity = $('#quantity').val();
var price = $('#price').val();
var discount = $('#discount').val();
var amount = $('#amount').val();
var dataString = 'barcode=' + barcode + '&productname=' + productname + '&sm=' + sm + '&spl=' + spl + '&quantity' + quantity + '&price=' + price + '&discount=' + discount + '&amount=' + amount;
$.ajax({
url: "http://localhost/retail/main/store",
type: "POST",
data: {
/*barcode: barcode,
productname: productname,
sm: sm,
spl: spl,
quantity: quantity,
price: price,
discount: discount,
amount: amount*/
data:$('#inventory_form').serialize()
},
success: function (res) {
alert(res);
}
});
这是我的Conroller代码:
public function store(){
/* echo ($_POST['barcode']);
echo "<br/>";
echo ($_POST['productname']);
echo "<br/>";
echo ($_POST['smsm']);
echo "<br/>";
echo ($_POST['spl']);
echo "<br/>";
echo ($_POST['quantity']);
echo "<br/>";
echo ($_POST['price']);
echo "<br/>";
echo ($_POST['discount']);
echo "<br/>";
echo ($_POST['amount']);
echo "<br/>";*/
//echo $this->input->post('barcode');
if ($this->session->userdata('status')== 1) {
for ($i = 0; $i < count($_POST['productname']); $i++) {
$order_id = $this->session->userdata('id');
$product_name = $this->input->post('productname')[$i];
$barcode = $this->input->post('barcode')[$i];
$spl= $this->input->post('spl')[$i];
$sm= $this->input->post('sm')[$i];
$quantity = $this->input->post('quantity')[$i];
$price = $this->input->post('price')[$i];
$discount =$this->input->post('discount')[$i];
$amount = $this->input->post('amount')[$i];
$data = array(
'order_id'=> $order_id,
'product_name'=>$product_name,
'barcode'=> $barcode,
'sm'=> $sm,
'spl'=> $spl,
'quantity'=>$quantity,
'price'=>$price,
'discount'=> $discount,
'amount'=> $amount,
);
$this->Sale_model->insert_sales_data($data);
echo "Data inserted successfully!";
}
}
else
{
redirect('login');
}
}
我正在使用Codeigniter。请告诉我,我上面提到的功能如何?
答案 0 :(得分:0)
为什么在ajax请求中获得单个值?
var barcode = $('#barcode option:selected').val();
var productname = $('#brcode_product').val();
var sm = $('#sm option:selected').val();
var spl = $('#spl').val();
var quantity = $('#quantity').val();
var price = $('#price').val();
var discount = $('#discount').val();
var amount = $('#amount').val();
为什么不只是序列化您的表单然后传递序列化数据?
答案 1 :(得分:0)
$('#save_btn').on('click', function () { var formdataString = $("#inventory_form"); $.ajax({ url: "http://localhost/retail/main/store", type: "POST", data: formdataString.serialize(), success: function (res) { alert(res); } }); });