我正在尝试使用PHP PDO将多个数组插入MySql数据库。它插入重复的行,而不是按我要求的方式插入。下面是我的代码
HTML
<div class="col-md-4">
<div class="form-group">
<label class="col-form-label"> Enter Product Name</label>
<input type="text" class="form-control" name="pname[]" placeholder="Product Name"/>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="col-form-label"> No. of Pieces</label>
<input type="text" class="form-control" name="pcount[]" placeholder="No.Of Items"/>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="col-form-label"> Estimated Amount</label>
<input type="text" class="form-control" name="estamount[]" placeholder="Estimated Amount of Each"/>
</div>
<p class="pull-left">Amount: <div class="Amount"></div></p>
</div>
PHP代码为
$totalamount=0;
foreach($_POST['pname'] as $proname){
foreach($_POST['pcount'] as $quantity){
foreach($_POST['estamount'] as $estamount){
$totalamount = $quantity*$estamount;
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO `order`(custname, contact, product_name, quantity, est_amount, advance_paid, delivery_period, date, status, total_amount, orderid) VALUES(:custname, :contact, :product_name, :quantity, :est_amount, :advance_paid, :delivery_period, :date, :status, :total_amount, :orderid)";
$query=$dbh->prepare($sql);
$query->bindParam(':custname',$custmorname,PDO::PARAM_STR);
$query->bindParam(':contact',$contact,PDO::PARAM_STR);
$query->bindParam(':product_name',$proname,PDO::PARAM_STR);
$query->bindParam(':quantity',$quantity,PDO::PARAM_STR);
$query->bindParam(':est_amount',$estamount,PDO::PARAM_STR);
$query->bindParam(':advance_paid',$advancepaid,PDO::PARAM_STR);
$query->bindParam(':delivery_period',$delevery,PDO::PARAM_STR);
$query->bindParam(':date',$date,PDO::PARAM_STR);
$query->bindParam(':status',$status,PDO::PARAM_STR);
$query->bindParam(':orderid',$orderid,PDO::PARAM_STR);
$query->bindParam(':total_amount',$totalamount,PDO::PARAM_STR);
$query->execute();
}
}
}
我想在一条记录中插入$proname , $quantity , $est_amount
,并且它应该根据插入的值插入多个重新编码
答案 0 :(得分:1)
由于您有一个嵌套的for循环,因此肯定会导致重复记录。让我快速纠正。
$totalamount=0;
for ($i = 0; $i < count($_POST['pname']); $i++){
$proname = $_POST['pname'][$i];
$quantity = $_POST['pcount'][$i];
$estamount = $_POST['estamount'][$i];
$totalamount = $quantity*$estamount;
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO `order`(custname, contact, product_name, quantity, est_amount, advance_paid, delivery_period, date, status, total_amount, orderid) VALUES(:custname, :contact, :product_name, :quantity, :est_amount, :advance_paid, :delivery_period, :date, :status, :total_amount, :orderid)";
$query=$dbh->prepare($sql);
$query->bindParam(':custname',$custmorname,PDO::PARAM_STR);
$query->bindParam(':contact',$contact,PDO::PARAM_STR);
$query->bindParam(':product_name',$proname,PDO::PARAM_STR);
$query->bindParam(':quantity',$quantity,PDO::PARAM_STR);
$query->bindParam(':est_amount',$estamount,PDO::PARAM_STR);
$query->bindParam(':advance_paid',$advancepaid,PDO::PARAM_STR);
$query->bindParam(':delivery_period',$delevery,PDO::PARAM_STR);
$query->bindParam(':date',$date,PDO::PARAM_STR);
$query->bindParam(':status',$status,PDO::PARAM_STR);
$query->bindParam(':orderid',$orderid,PDO::PARAM_STR);
$query->bindParam(':total_amount',$totalamount,PDO::PARAM_STR);
$query->execute();
}