2017-09-27 88 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。核实。