hi all i am trying to insert / update data in a table using 2 nested foreach loops... i have tables -
1. temp
vendor_ID | component | Qty
2. stock
stock_ID | component | Qty
$query2 = "SELECT * FROM sample.temp";
$rslt = $dbo->query($query2);
if($rslt->rowCount() > 0)
{
foreach($rslt as $item)
{
$Qty = $item['Qty'];
$component = $item['component'];
$vendor_ID = $item['vendor_ID'];
$query5 = "SELECT * FROM sample.stock";
$reslt = $dbo->query($query5);
if($reslt->rowCount() > 0)
{
foreach($reslt as $itm)
{
$Qty1 = $itm['Qty'];
$stock_ID = $itm['stock_ID'];
$component1 = $itm['component'];
if(($vendor_ID!=$stock_ID && $component!=$component1) || ($vendor_ID!=$stock_ID && $component==$component1) || ($vendor_ID==$stock_ID && $component!=$component1))
{
$query6 = "INSERT INTO sample.stock (stock_ID, component, Qty) VALUES ($vendor_ID, '$component', $Qty)";//inserting new entry
if ($dbo->query($query6))
{echo "Data inserted !";}
else {echo "Production not updated!";}
}
else { $query4 = "UPDATE sample.stock SET Qty=(Qty+$Q) WHERE stock_ID=$vendor_ID AND component='$component'";//updating single existing entries
if ($dbo->query($query4))
{echo "Production updated !";}
else {echo "Production not updated!";}}
}
}
}
}
Firstly i select a temporary table from where i transfer data into stock table on the basis of 3 checks -
updating existing values in stock table if they have same stock_ID and component as it is in temporary table
inserting new entry in stock table if it is not having the component corresponding to that stock_ID
inserting new entry in stock table if it is having the component but does not corresponds to any stock_ID in stock table
Note- vendor_ID in temporary table is same as stock_ID in stock table.