简体   繁体   English

如何使用PHP PDO将具有多行的多个数组插入MySQL

[英]how to Insert multiple arrays with multiple rows into MySQL using PHP PDO

I am trying to insert multiple arrays into MySql database using PHP PDO. 我正在尝试使用PHP PDO将多个数组插入MySql数据库。 Its inserting duplicated row and it is not inserting the way I required. 它插入重复的行,而不是按我要求的方式插入。 below is my code 下面是我的代码

HTML 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: &nbsp;<div class="Amount"></div></p>
                </div>

And PHP Code is 和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();
                    }
              }
        }

I would like to insert $proname , $quantity , $est_amount in one record and its should insert multiple recodes as per inserted values 我想在一条记录中插入$proname , $quantity , $est_amount ,并且应该根据插入的值插入多个重新编码

As you have a nested for loop it will definitely lead for duplicate records. 由于您有嵌套的for循环,因此肯定会导致重复记录。 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();


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM