2017-09-27 15 views
1

我是新來的,在stackoverflow上。在while循環上執行select查詢後,並非所有行都被插入到表中 - PHP/MYSQL

我正在執行一個選擇查詢來填充我需要的輸出。當它從查詢中提取行時,每個已經被提取的行都是我將它們插入到一個特定的表中。但是,我需要實現的確切數量的行是1,767行,但執行查詢後,1759是輸出。我有8行丟失。

我的代碼有什麼問題?

這裏是我的代碼:

$query2 = "SELECT trihadjustmentitems.AdjID AS `adjid1`, trihadjustment.Adj_ID AS `adjid2`, 
trihadjustment.AdjToUnitID AS `adjtounitid`, trihadjustment.AdjDate AS `adjdate`, trihadjustmentitems.InvItemsID AS `invitemid`, 
trihadjustmentitems.SlsItemsID AS `slsitemid`, trihadjustmentitems.RecipeID AS `recipeid`, trihadjustmentitems.Remark AS `remark`, 
trihadjustmentitems.AdjQty AS `adjqty`, 
trihadjustment.StockCenter_ID AS `stockcenterid1`, trihadjustmentitems.StockCenter_ID AS `stockcenterid2` 
FROM trihadjustmentitems 
INNER JOIN trihadjustment ON trihadjustmentitems.AdjID = trihadjustment.Adj_ID"; 

     $result2 = mysqli_query($connection, $query2); 


    while($row2 = mysqli_fetch_array($result2)) 
    { 
     $query3 = "INSERT INTO adjustments (adjid1, adjid2, adjtounitid, adjdate, invitemid, slsitemid, recipeid, remark, adjqty, stockcenterid1, stockcenterid2) VALUES ('$row2[adjid1]', '$row2[adjid2]', '$row2[adjtounitid]', '$row2[adjdate]', '$row2[invitemid]', '$row2[slsitemid]', '$row2[recipeid]', '$row2[remark]', '$row2[adjqty]', '$row2[stockcenterid1]', '$row2[stockcenterid2]')"; 
     $result3 = mysqli_query($connection, $query3); 
    } 
+0

可能是因爲您沒有使用參數化查詢或轉義值(mysqli_real_escape_string)這些值而導致8行中包含單個qoute(')的SQL查詢('),這也有助於防止SQL注入攻擊。 –

回答

-1
INSERT INTO tbl_name 
(a,b,c) 
VALUES 
(1,2,3), 
(4,5,6), 
(7,8,9); 

沒有必要在循環中運行查詢,您可以循環完成後,試試這個

0

有沒有足夠的信息來確定任何錯誤都。您的代碼儘管有日期,但不包含任何可幫助任何人確定哪裏出錯的東西,如果有任何錯誤。當談到數字時,我們只能相信你,這不是IT的工作方式。

根本不需要你的查詢。您可以在沒有while循環的情況下執行操作。只需使用INSERT INTO ... SELECT語法。

我會用你的代碼來說明

$query = <<<EOF 
INSERT INTO adjustments 

(adjid1, adjid2, adjtounitid, adjdate, invitemid, slsitemid, recipeid, remark, adjqty, stockcenterid1, stockcenterid2) 

SELECT 
    trihadjustmentitems.AdjID AS `adjid1`, 
    trihadjustment.Adj_ID AS `adjid2`, 
    trihadjustment.AdjToUnitID AS `adjtounitid`, 
    trihadjustment.AdjDate AS `adjdate`, 
    trihadjustmentitems.InvItemsID AS `invitemid`, 
    trihadjustmentitems.SlsItemsID AS `slsitemid`, 
    trihadjustmentitems.RecipeID AS `recipeid`, 
    trihadjustmentitems.Remark AS `remark`, 
    trihadjustmentitems.AdjQty AS `adjqty`, 
    trihadjustment.StockCenter_ID AS `stockcenterid1`, 
    trihadjustmentitems.StockCenter_ID AS `stockcenterid2` 

FROM trihadjustmentitems 

INNER JOIN trihadjustment ON trihadjustmentitems.AdjID = trihadjustment.Adj_ID 

EOF; 

$result = mysqli_query($query); 

if(!result) { 
    echo 'An error occurred'; 
} 
-1

它似乎並沒有成爲一個PHP的問題。
問題可能是INNER JOIN,你可以使用,而不是LEFT JOIN爲了從trihadjustmentitems表中提取的所有記錄。 我希望這個幫助。

-1

首先,爲什麼$ result1,$ result2,$ result3?...和所有變量一樣。

而不是while,你可以使用foreach()循環。

foreach($resultQuery as $result){ 
    $insertQuery = "INSERT INTO adjustments... 
} 

use var_dump($ result);如果你不知道它的結果是什麼。

確保所有結果具有相同的結構。也許你不能在調整中插入某些東西,因爲該值爲NULL。覈實。