我是关于stackoverflow的新人。
我正在执行选择查询以填充我需要的输出。当它从查询中获取行时,已经获取的每一行都是我将它们插入到特定的表中。但是,我需要实现的确切行数是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 :(得分: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 :(得分:-1)
INSERT INTO tbl_name
(a,b,c)
VALUES
(1,2,3),
(4,5,6),
(7,8,9);
无需在循环中运行查询,您可以在循环完成后尝试此操作
答案 2 :(得分:-1)
它似乎不是一个PHP问题 问题可能是 INNER JOIN ,您可以使用 LEFT JOIN 来提取 trihadjustmentitems 表中的所有记录。 我希望这有帮助。
答案 3 :(得分:-1)
首先,为什么$ result1,$ result2,$ result3?...和所有变量相同。
而不是while,你可以使用foreach()循环。
foreach($resultQuery as $result){
$insertQuery = "INSERT INTO adjustments...
}
使用var_dump($ result);如果你不知道它的结果是什么。
确保所有结果具有相同的结构。也许你不能在调整中插入一些东西,因为值为NULL。检查一下。