在while循环上执行select查询后,并非所有行都插入到表中 - PHP / MYSQL

I'm new here on stackoverflow.

I'm performing a select query to populate the output I need. While it is fetching rows from query, every rows that has been fetched is I'm inserting them into a specific table. But however, the exact amount of rows that I need to achieve is 1,767 rows but after performing the query, 1759 was the output. I have 8 rows missing.

What's the problem with my code?

Here's my code:

$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);
    }
INSERT INTO tbl_name 
(a,b,c) 
VALUES
(1,2,3),
(4,5,6),
(7,8,9);

there is no need to run query in loop you can try this after loop complete

There's not enough information to determine that anything goes wrong at all. Your code, although dated, doesn't contain anything that can help anyone determine what's wrong, if anything is wrong. We can only trust you when it comes to numbers and that's not how IT works.

Your query is not needed at all. You can do what you're after without the while loop. Simply use INSERT INTO ... SELECT syntax.

I'll use your code to illustrate

$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';
}

It don't seem to be a PHP problem.
The problem could be the INNER JOIN, you could use instead LEFT JOIN in order to extract all the record from trihadjustmentitems table. I hope this help.

First, why $result1, $result2, $result3?... And the same with all vars.

Instead of while, you could use foreach() loop.

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

use var_dump($result); if you don't know what its $result.

Make sure all the results have the same structure. Maybe you can't insert something in adjustment because the value is NULL. Check it.