尝试使用解决方案尝试了多个问题,但都没有解决我的问题。
此表单使用JQuery动态添加一组字段。问题是MySQL在提交时没有插入数据集。这是代码:
HTML:
<form action="new_purchase_order%20(1).php" method="post">
Supplier
<select name="supplier[]">
<option value="">Select supplier...</option>
<?php
$get_supps = "select supplier_name from suppliers";
$supplist = mysqli_query($db, $get_supps);
//get suppliers available in the database
while($row = mysqli_fetch_assoc($supplist)) {
?>
<option value="<?php echo $row['supplierID'] ?>"><?php echo $row['supplier_name'] ?></option>
<?php } ?>
</select><br /><br />
<select name="item[]">
<option value="">Select item...</option>
<?php
$get_items = "select name from raw_materials";
$itemlist = mysqli_query($db, $get_items);
//get all raw materials available in the database
while($row = mysqli_fetch_assoc($itemlist)) {
?>
<option value="<?php echo $row['materialID'] ?>"><?php echo $row['name'] ?></option>
<?php } ?>
</select>
<!--enter its unit price-->
<input type="text" name="price[]" placeholder="Unit Price">
<!--enter the qty of the item selected-->
<input type="number" name="qty[]" min=1 placeholder="Qty" width="30px">
<a href="javascript:void(0)" class="button addMore">ADD</a>
PHP:
<?php
include("session.php");
if (isset($_POST['submit'])) {
$supplier = $_POST['supplier'];
$item = $_POST['item'];
$qty = $_POST['qty'];
$price = $_POST['price'];
for ($i=0; $i < count($item); $i++) {
$supplier[$i] = mysqli_real_escape_string($db, $supplier[$i]);
$item[$i] = mysqli_real_escape_string($db, $item[$i]);
$qty[$i] = mysqli_real_escape_string($db, $qty[$i]);
$price[$i] = mysqli_real_escape_string($db, $price[$i]);
if (count($item) >= 0 && count($item) <= 24) {
mysqli_query($db, "insert into purchase_order_history values('', '{$item[$i]}', '{$qty[$i]}', '{$price[$i]}', '', '$supplier[$i]', 'Pending')");
echo "Success";
}
else {
echo "Error";
}
}
}
?>
基本上它使用item,qty和price的数组。我的问题是尽管PHP没有错误,它根本不插入任何东西。查询是:
插入purchase_order_history值('','{$ item [$ i]}','{$ qty [$ i]}','{$ price [$ i]}','','$供应商[$ i]','待定');
编辑:这是JQuery代码:
<!-- jQuery library -->
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Bootstrap js library -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
//group add limit
var maxGroup = 24;
//add more fields group
$(".addMore").click(function(){
if($('body').find('.form').length < maxGroup){
var fieldHTML = '<div
class="form">'+$(".formCopy").html()+'</div>';
$('body').find('.form:last').after(fieldHTML);
}else{
alert('Only '+maxGroup+' items are allowed.');
}
});
//remove fields group
$("body").on("click",".remove",function(){
$(this).parents(".form").remove();
});
});
</script>
查询本身是否有错误?或者是导致问题的数组编码?