我是新来的,在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);
}
可能是因为您没有使用参数化查询或转义值(mysqli_real_escape_string)这些值而导致8行中包含单个qoute(')的SQL查询('),这也有助于防止SQL注入攻击。 –