2017-01-23 84 views
0

enter image description here我想插入多個文本框只有兩個名稱。在所有這些texboxes我有名稱項目[]和數量[]。我試圖嵌套foreach循環,但增加了比預期更多的值。問題是$ _POST ['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; 
      } 

回答

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(); 
    } 

} 

由於項目是對應於它們的數量(糾正我,如果我錯了)

+0

是,$項目= $ _ POST [「項目」],因爲輸入的名稱是項目[],一個添加上述值nd $ qty = $ _ POST ['qty'],輸入qty []。 – MaxPower

+0

我編輯了代碼,希望它可以幫助 – cjatstackoverflow

0

我發現如何從多個輸入字段項目[]和數量[],

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(); 

         } 

       } 
相關問題