2017-08-16 150 views
1

我想添加一些靜態行和多行同時到mysql以及發票。我可以將行添加到mysql中。但是,當我添加多行然後插入第一行所有字段完全但不插入所有字段從其他行,只插入動態字段到MySQL。例如規範,數量插入不錯,但pur,appb,日期,appn不從第二行插入。任何人都可以幫助我。 我的代碼都低於...通過php插入多行到mysql

<form action="ps.php" method="POST"> 
     <table id="customers"> 
     <tr> 
     <td>Project/Purpose</td><td><input type="text" name="pur[]" required="required" /></td> 
     <td>Approved Budget:</td><td><input type="text" name="appb[]" required="required" /></td> 
     </tr> 
     <tr> 
     <td>Expected Delivery Date:</td><td><input type="date" name="expdate[]" required="required" /></td> 
     </tr> 

     <td>Approve by:</td><td><input type="text" name="appn[]" required="required" /></td> 
     </tr> 
     </table> 

     <fieldset style="background-color:#00FFFF"><h2>Purchase Information</h2></fieldset> 


    <div id="page-wrap"> 
    <table id="customers"> 

      <tr> 
       <th>Goods/Services(include description and specifications)</th> 
       <th>Qty Required(Goods only)</th> 
       <th>Qty on Hand(Goods only)</th>    
       <th>Qty to Order</th> 
       <th>Est. Unit Price</th> 
       <th>Total Estimated Cost</th> 
      </tr> 

      <!-- add remove row dynamically --> 
      <tr class="item-row"> 
       <td class="item-name"><div class="delete-wpr"><a class="delete" href="javascript:;" title="Remove row">X</a><textarea class="noscrollbars" name="specification[]" onkeyup="autoGrow(this);"></textarea></div></td> 
       <td ><input type="number" name="" /></td> 
       <td ><input type="number" name="" /></td> 
       <td><input type="number" class="qty" name="qty[]" /></td> 
       <td><input type="number" class="cost" name="esp[]" /> </td> 

       <td><span class="price">0.00</span></td> 
      </tr> 

      <!-- add remove row dynamically --> 

      <tr class="item-row"> 
       <td class="item-name"><div class="delete-wpr"><a class="delete" href="javascript:;" title="Remove row">X</a><textarea class="noscrollbars" name="specification[]" onkeyup="autoGrow(this);"></textarea></div></td> 
       <td ><input type="number" name="" /></td> 
       <td ><input type="number" name="" /></td> 
       <td><input type="number" class="qty" name="qty[]" /></td> 
       <td><input type="number" class="cost" name="esp[]" /> </td> 
       <input type="hidden" name="empid[]" value='<?php echo $empid;?>' /> 
       <td><span class="price">0.00</span></td> 

      </tr> 

      <tr id="hiderow"> 
      <td colspan="6"><a id="addrow" href="javascript:;" title="Add a row">Add a row</a></td> 
      </tr> 

      <tr> 
       <td colspan="4" class="blank"> </td> 
       <td colspan="1" class="total-line">Total</td> 
       <td class="total-value"><div id="total">0.00</div></td> 
      </tr> 
      <tr> 
       <td colspan="4" class="blank"> </td> 
       <td colspan="1" class="total-line"><input type="submit" name="submit" value="Submit" /></td> 

       <td class="blank"><textarea id="paid" style="display:none;" ></textarea></td> 
      </tr> 

     </table> 



    </div> 



    </form> 

ps.php

if(isset($_POST)==true && empty($_POST)==false): 
       $pur = $_POST['pur'];   
       $appb=$_POST['appb']; 
       $expdate=$_POST['expdate'];   

       $appn=$_POST['appn']; 
       $specification=$_POST['specification'];     
       $qty=$_POST['qty']; 
       $esp=$_POST['esp'];        
       $empid=$_POST['empid']; 

       foreach($qty as $a => $b){     



        $result = mysqli_query($mysqli, "INSERT INTO `pr`(`pur`, `empid`, `appb`, `expdate`, `appn`, `specification`, `qty`, `esp`) VALUES ('$pur[$a]','$empid[$a]','$appb[$a]','$expdate[$a]','$appn[$a]','$specification[$a]','$qty[$a]','$esp[$a]')");  

        } 

        echo "Your entry has been successfully recorded, Thank you!";  
      else: echo "Some things went wrong please try again"; 
      endif; 
+1

瞭解準備好的語句以防止SQL注入 – Jens

+1

首先,您的代碼對SQL注入開放,所以您可能試圖執行該SQL語句中的任何內容。這導致了這一點...當查詢失敗時,查詢是什麼?什麼是MySQL的錯誤?你目前沒有檢查這些東西。 – David

回答

1

只是字段名稱和使用破滅功能,而將數據做成了。

例如:

$data['name'] = 'abc'; 
    $data['description'] = 'description'; 
    $data['created'] = date("Y-m-d H:i:s"); 

查詢:

 $fields = array_keys($data); 
     $sql = "INSERT INTO table_name . (`" . implode('`,`', $fields) . "`) 
       VALUES('" . implode("','", $data) . "')"; 
2

試試這個:

$qry = "INSERT INTO `pr`(`pur`, `empid`, `appb`, `expdate`, `appn`, `specification`, `qty`, `esp`) VALUES "; 
$i = 0; 
foreach($qty as $a => $b) {     

    $qry .= "('$pur[$a]','$empid[$a]','$appb[$a]','$expdate[$a]','$appn[$a]','$specification[$a]','$qty[$a]','$esp[$a]')"; 
    if($i == count($qty)) { 
     $qry .= ";"; 
    } else { 
     $qry .= ","; 
    } 

$i++; 
} 

$result = mysqli_query($mysqli, $qry); 

如果您有大量的數據,然後使用插入查詢一次,它會提高效率。