如何插入具有两个不同名称的多个文本框?

时间:2017-01-23 17:28:56

标签: php mysql pdo

enter image description here我正在尝试插入只有两个名字的多个文本框。在所有这些texbox中,我有名称items []和qty []。我试图嵌套foreach循环,但添加了比预期更多的值。问题在于$ _POST ['qty']。我可以从items []中选择和添加,但我无法添加qty整数值!

<div class="col-md-12 diff">
                    <div class="col-md-4">
                        <p>Select Item</p>
                       <input style="color:black;" type="text" class="form-control items" name="items[]" placeholder="Search...">
                    <div class="side"></div>
                    </div>
                    <div class="col-md-2">
                        <p>QTY</p>
                        <input id="pats_input" class="form-control pats_tb" type="text" name="qty[]" placeholder="NO:">
                    <div class="side"></div>
                    </div>
                </div>
                <div class="col-md-12 diff">
                    <div class="col-md-4">
                        <p>Select Item</p>
                       <input style="color:black;" type="text" class="form-control items" name="items[]" placeholder="Search...">
                    <div class="side"></div>
                    </div>
                    <div class="col-md-2">
                        <p>QTY</p>
                        <input id="pats_input" class="form-control pats_tb" type="text" name="qty[]" placeholder="NO:">
                    <div class="side"></div>
                    </div>
                </div>


function issueToEmployee(){
            global $conn;
           if(isset($_POST['pats']) && $_POST['pats'] !="" && isset($_POST['items']) && $_POST['items'] !="" && isset($_POST['qty'])){
                $perstat = new getPerstat();
                //get employee pats
                $perstat->getPats($_POST['pats']);
                $stock = new StockTable();
                $qty = $_POST['qty'];
                foreach($_POST['items'] as $item){
                    foreach($qty as $q){
                        if(!empty($item) && !empty($q)){
                             $stock->getItemByName($item);
                        $sql = $conn->prepare("INSERT INTO issues (empid, itemid) VALUES('$perstat->id','$stock->itemid')");
                        $sql->execute();
                        }
                    }

                }

                return true;

            }else{
                return false;
            }

2 个答案:

答案 0 :(得分:0)

我认为你正试图像这样指出数量上的项目:

$items = $_POST['items'];
$qty = $_POST['qty'];
foreach($items as $i => $item)
{
 if(!empty($item) && (isset($qty[$i]) && !empty($qty[$i])))
 {
    $stock->getItemByName($item);
    $sql = $conn->prepare("INSERT INTO issues (empid,itemid) VALUES('$perstat->id','$stock->itemid')");
    $sql->execute();
  }

}

因为项目对应于他们的数量(如果我错了,请纠正我)

答案 1 :(得分:0)

我找到了如何从多个输入字段items []和qty [],

添加上述值
for($i = 0; $i < 8 ; $i++){
                       //$id = $_POST['id'][$i];
                     $description =  $_POST['items'][$i];
                       $qty = $_POST['qty'][$i];
                        $date = $_POST['date'];
                       if(!empty($qty)){
                         $stock->getItemByName($description);
                        $sql = $conn->prepare("INSERT INTO issues (empid, itemid, qty, date) VALUES('$perstat->id','$stock->itemid','$qty','$date')");
                          $sql->execute();

                       }

                }