Reputation: 2268
I am trying to insert multiple arrays into MySql database using PHP PDO. Its inserting duplicated row and it is not inserting the way I required. below is my code
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>
And PHP Code is
$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();
}
}
}
I would like to insert $proname , $quantity , $est_amount
in one record and its should insert multiple recodes as per inserted values
Upvotes: 0
Views: 797
Reputation: 1933
As you have a nested for loop it will definitely lead for duplicate records. Let me make a quick correction.
$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();
}
Upvotes: 1