简体   繁体   English

如何使用mysql查询在php中使用嵌套的foreach循环

[英]how to use nested foreach loops in php using mysql queries

hi all i am trying to insert / update data in a table using 2 nested foreach loops... i have tables - 大家好,我正在尝试使用2个嵌套的foreach循环在表中插入/更新数据...我有表-

1. temp 1.温度

vendor_ID | vendor_ID | component | 组件 | Qty 数量

2. stock 2.库存

stock_ID | 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 - 首先,我基于3张支票从中选择一个临时表,从该表中将数据传输到库存表中-

  1. updating existing values in stock table if they have same stock_ID and component as it is in temporary table 如果库存表中的现有值具有与临时表中相同的stock_ID和组件,则更新它们中的现有值

  2. inserting new entry in stock table if it is not having the component corresponding to that stock_ID 如果没有对应于stock_ID的组件,则在库存表中插入新条目

  3. inserting new entry in stock table if it is having the component but does not corresponds to any stock_ID in stock table 如果具有组件但与库存表中的任何stock_ID不对应,则在库存表中插入新条目

Note - vendor_ID in temporary table is same as stock_ID in stock table. 注意 -临时表中的vendor_ID与库存表中的stock_ID相同。

A single foreach would be of more use, you don't need a nested foreach query. 单个foreach会更有用,您不需要嵌套的foreach查询。

$sql = "SELECT * FROM sample.temp";
$query = $dbo->prepare($sql);
$query->execute();

$result = $dbo->fetchAll(PDO::FETCH_ASSOC);
if(!empty($result)) {
    foreach($result as $item) {
    $Qty = $item['Qty'];
    $component = $item['component'];
    $vendor_ID = $item['vendor_ID'];

    $sql = "SELECT * FROM sample.stock WHERE stock_ID = :vendor_ID";
    $query = $dbo->prepare($sql);
    $query->bindValue(':temp_stock', $vendor_ID);
    $query->execute();
    $result = $query->fetch(PDO::FETCH_ASSOC);

        if (!empty($result)) {
            $sql = "UPDATE sample.stock SET Qty = (Qty + :Qty) WHERE stock_ID = :vendor_ID AND component= :component";
            $query = $dbo->prepare($sql);
            $query->bindValue(':vendor_ID', $vendor_ID);
            $query->bindValue(':component', $component);
            $query->bindValue(':Qty', $Qty);
            $query->execute();

            if ($query->rowCount > 0) {
            echo "Success: Updated $vendor_ID quantity";
            } else {
            echo "Error: Failed to modify $vendor_ID quantity";
            }

        } else {
            $sql = "INSERT INTO sample.stock (stock_ID, component, Qty) VALUES ($vendor_ID, '$component', $Qty)";
            $query = $dbo->prepare($sql);
            $query->bindValue(':vendor_ID', $vendor_ID);
            $query->bindValue(':component', $component);
            $query->bindValue(':Qty', $Qty);
            $query->execute();

            if ($query) {
            echo "Success: Added $vendor_ID";
            } else {
            echo "Error: Failed to add $vendor_ID";
            }
        }
    }
} else {
echo "No results in temp table";
}`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM