我有一个由javascript.am动态添加texfeild的表单,使用数组提取php文件中的textfeild值。一切似乎都很好。但是我很难将数据保存到数据库中。请帮忙。下面是我的代码。
在此处形成值
<form method="POST" id="theForm" action="invpdf.php"><div id="fields">
<input type="text" name="fields[barcode][]"/>
<input type="text" name="fields[description][]"/>
<input type="text" id="unity"name="fields[unitprice][]" />
<input type="text" id="qty"name="fields[qty][]"/>
<input type="text" id="total"name="fields[total][]"/>
<br/>
</div>
<input type="button" id="addField" value="Add Field"/></form>
javascript添加textfeilds
<script>
$(document).ready(function() {
$('#addField').click(function() {
$('#fields').append(
$('<input type="text" name="fields[barcode][]"/>'),
$('<input type="text" name="fields[description][]"/>'),
$('<input type="text" name="fields[unitprice][]"/>'),
$('<input type="text" name="fields[qty][]"/>'),
$('<input type="text" name="fields[total][]"/>'),
$('<br/>')
);
})
});
</script>
我的php处理如下面的代码
$numemma = count($_POST['fields']['barcode']);
for ($i = 0; $i < $numemma; $i++) {
$field = array(
'barcode' => $_POST['fields']['barcode'][$i],
'description' => $_POST['fields']['description'][$i],
'unitprice' => $_POST['fields']['unitprice'][$i],
'qty' => $_POST['fields']['qty'][$i],
'total' => $_POST['fields']['total'][$i]
);
$feeilds = '`'.implode('`,`', $field).'`';
$data = '\''.implode('\',\'',$field).'\'';
mysqli_query($conn,"INSERT INTO `sales`($feeilds)VALUES($data)");
}