全体像

PHP
<?php
$dbh = db_open();
//edit and delete一覧
$sql = "SELECT
id,
code,
maker,
item,
mail,
basic_quantity,
usage_amount,
price,
capacity,
COALESCE(quantity_per_rot, 0) AS qpr
FROM rot";
$stmt = $dbh->prepare($sql);
$stmt->execute();
$lists = $stmt->fetchAll(PDO::FETCH_ASSOC);


//各店舗に商品マスタと同時に登録するために用意する$shop
function getShops($dbh) {
    $shops = [];
    try {
        $sql = "SELECT * FROM shops"; // `shops`は店舗情報が格納されているテーブル名です。
        foreach ($dbh->query($sql) as $row) {
            $shops[] = [
                'shop_id' => $row['shop_id'],
            ];
        }

    } catch (PDOException $e) {
        echo "Error: " . $e->getMessage();
    }

    return $shops;
}


//商品マスタに登録
if (isset($_POST['submit'])) {
    
    // 入力された値が存在するかを確認
    $code = isset($_POST['code']) ? $_POST['code'] : null;
    $maker = isset($_POST['maker']) ? $_POST['maker'] : null;
    $item = isset($_POST['item']) ? $_POST['item'] : null;
    $mail = isset($_POST['mail']) ? $_POST['mail'] : null;
    $basic_quantity = isset($_POST['basic_quantity']) ? $_POST['basic_quantity'] : null;
    $usage_amount = isset($_POST['usage_amount']) ? $_POST['usage_amount'] : null;
    $price = isset($_POST['price']) ? $_POST['price'] : null;
    $capacity = isset($_POST['capacity']) ? $_POST['capacity'] : null;
    $quantity_per_rot = isset($_POST['quantity_per_rot']) ? $_POST['quantity_per_rot'] : null;

    // 全ての値が存在するか確認
    if ($code !== null && $maker !== null && $item !== null && $mail !== null && $basic_quantity !== null && $usage_amount !== null && $price !== null && $capacity !== null && $quantity_per_rot !== null) {
        $date = date("Y-m-d");

        try{
            
        $dbh->beginTransaction();
        // 商品マスタに登録
        $m_sql = "INSERT INTO mastar_table (date,code, maker, item, mail, basic_quantity, usage_amount, price, capacity, quantity_per_rot) 
        VALUES (:date,:code, :maker, :item, :mail, :basic_quantity, :usage_amount, :price, :capacity, :quantity_per_rot)";
        
        // PDOオブジェクトを使用してSQL文を実行
        $m_stmt = $dbh->prepare($m_sql);
        
        $m_stmt->bindParam(':date', $date, PDO::PARAM_STR);
        $m_stmt->bindParam(':code', $code, PDO::PARAM_INT);
        $m_stmt->bindParam(':maker', $maker, PDO::PARAM_STR);
        $m_stmt->bindParam(':item', $item, PDO::PARAM_STR);
        $m_stmt->bindParam(':mail', $mail, PDO::PARAM_STR);
        $m_stmt->bindParam(':basic_quantity', $basic_quantity, PDO::PARAM_INT);
        $m_stmt->bindParam(':usage_amount', $usage_amount, PDO::PARAM_STR);
        $m_stmt->bindParam(':price', $price, PDO::PARAM_INT);
        $m_stmt->bindParam(':capacity', $capacity, PDO::PARAM_STR);
        $m_stmt->bindParam(':quantity_per_rot', $quantity_per_rot, PDO::PARAM_INT);

        // SQL文を実行
        $m_stmt->execute();

        // 最後に挿入されたIDを取得
        //stock_inputのrot_idへ登録
        $lastInsertId = $dbh->lastInsertId();

        // 各店舗の在庫テーブルにも追加
        $shops = getShops($dbh); // 店舗情報を取得する関数
        foreach($shops as $shop) {
        $s_sql = "INSERT INTO stock (date, code, maker, item, price, basic_quantity, shop_id) 
        VALUES (:date, :code, :maker, :item, :price, :basic_quantity, :shop_id)";    
        $s_stmt = $dbh->prepare($s_sql);
        $s_stmt->bindParam(':date', $date);
        $s_stmt->bindParam(':code', $code);
        $s_stmt->bindParam(':maker', $maker);
        $s_stmt->bindParam(':item', $item);
        $s_stmt->bindParam(':price', $price);
        $s_stmt->bindParam(':basic_quantity', $basic_quantity);
        $s_stmt->bindParam(':shop_id', $shop['shop_id']);

        $s_stmt->execute();
        }
        
    $dbh->commit();

    header('Location:index.php');
    exit;
    } catch (PDOException $e) {
    $dbh->rollback(); // トランザクションロールバック
    echo "Error: " . $e->getMessage();
    
    }

    } else {
        echo "全てのフィールドが入力されていません。";
    }
    
}
?>
HTML
<form action="" method="post">
 <p>メーカー名</p>
 <p><input type="text" name="maker"></p>
                        
 <p>商品コード</p>
 <p><input type="text" name="code"></p>
                        
 <p>商品名</p>
 <p><input type="text" name="item"></p>
                        
 <p>発注ロット</p>
 <p><input type="text" name="quantity_per_rot"></p>
                        
 <p>基本在庫数</p>
 <p><input type="text" name="basic_quantity"></p>
                       
 <p>金額</p>
 <p><input type="text" name="price"></p>
   
 <p>容量</p>
 <p><input type="text" name="capacity"></p>
                       
 <p>1杯使用量</p>
 <p><input type="text" name="usage_amount"></p>
                        
 <p>メール宛先</p>
 <p><input type="text" name="mail"></p>
                       
 <input type="submit" name="submit" value="保存" class="ma_top_2">
                        
 </form>
                  
 <table >
 <tbody>
 <tr>
 <th>メーカー名</th>
 <th>商品コード</th>
 <th>商品名</th>
 <th>発注ロット</th>
 <th>基本在庫数</th>
 <th>金額</th>       
 <th>容量</th>  
 <th>1杯使用量</th>
 <th>メール宛先</th>
 </tr>
 <?php foreach($lists as $list){ ?>
  <tr>
  <td><?php echo $list['maker'];?></td>
  <td><?php echo $list['code'];?></td>
  <td><?php echo $list['item']; ?></td>
  <td><?php echo $list['qpr'];?></td>
  <td><?php echo $list['basic_quantity']; ?></td>
  <td>¥<?php echo number_format($list['price']);?></td>
  <td><?php echo $list['capacity'];?></td>
  <td><?php echo $list['usage_amount']; ?></td>
  <td><?php echo $list['mail'];?></td>
  <td><a href="edit.php?id=<?php echo $list['id']; ?>" class="h_detail de">編集</a></td>
  
<form action="delete.php" method="POST">
   <input type="hidden" name="id" value="<?php echo $list['id'];?>">
   <td><button type="submit" class="h_detail">削除</button></td>
</form>
</tr>
<?php } ?>
                    
</tbody>
</table>

説明

PHP
<?php
$dbh = db_open();

//商品一覧表示
$sql = "SELECT
id,
code,
maker,
item,
mail,
basic_quantity,
usage_amount,
price,
capacity,
COALESCE(quantity_per_rot, 0) AS qpr
FROM rot";
$stmt = $dbh->prepare($sql);
$stmt->execute();
$lists = $stmt->fetchAll(PDO::FETCH_ASSOC);


//各店舗に商品マスタと同時に登録するために用意する$shop
function getShops($dbh) {
    $shops = [];
    try {
        $sql = "SELECT * FROM shops"; // `shops`は店舗情報が格納されているテーブル名です。
        foreach ($dbh->query($sql) as $row) {
            $shops[] = [
                'shop_id' => $row['shop_id'],
            ];
        }

    } catch (PDOException $e) {
        echo "Error: " . $e->getMessage();
    }

    return $shops;
}


//商品マスタに登録
if (isset($_POST['submit'])) {
    
    // 入力された値が存在するかを確認
    $code = isset($_POST['code']) ? $_POST['code'] : null;
    $maker = isset($_POST['maker']) ? $_POST['maker'] : null;
    $item = isset($_POST['item']) ? $_POST['item'] : null;
    $mail = isset($_POST['mail']) ? $_POST['mail'] : null;
    $basic_quantity = isset($_POST['basic_quantity']) ? $_POST['basic_quantity'] : null;
    $usage_amount = isset($_POST['usage_amount']) ? $_POST['usage_amount'] : null;
    $price = isset($_POST['price']) ? $_POST['price'] : null;
    $capacity = isset($_POST['capacity']) ? $_POST['capacity'] : null;
    $quantity_per_rot = isset($_POST['quantity_per_rot']) ? $_POST['quantity_per_rot'] : null;

    // 全ての値が存在するか確認
    if ($code !== null && $maker !== null && $item !== null && $mail !== null && $basic_quantity !== null && $usage_amount !== null && $price !== null && $capacity !== null && $quantity_per_rot !== null) {
        $date = date("Y-m-d");

        try{
            
        $dbh->beginTransaction();
        // 商品マスタ
        $m_sql = "INSERT INTO mastar_table (date,code, maker, item, mail, basic_quantity, usage_amount, price, capacity, quantity_per_rot) 
        VALUES (:date,:code, :maker, :item, :mail, :basic_quantity, :usage_amount, :price, :capacity, :quantity_per_rot)";
        
        // PDOオブジェクトを使用してSQL文を実行
        $m_stmt = $dbh->prepare($m_sql);
        
        $m_stmt->bindParam(':date', $date, PDO::PARAM_STR);
        $m_stmt->bindParam(':code', $code, PDO::PARAM_INT);
        $m_stmt->bindParam(':maker', $maker, PDO::PARAM_STR);
        $m_stmt->bindParam(':item', $item, PDO::PARAM_STR);
        $m_stmt->bindParam(':mail', $mail, PDO::PARAM_STR);
        $m_stmt->bindParam(':basic_quantity', $basic_quantity, PDO::PARAM_INT);
        $m_stmt->bindParam(':usage_amount', $usage_amount, PDO::PARAM_STR);
        $m_stmt->bindParam(':price', $price, PDO::PARAM_INT);
        $m_stmt->bindParam(':capacity', $capacity, PDO::PARAM_STR);
        $m_stmt->bindParam(':quantity_per_rot', $quantity_per_rot, PDO::PARAM_INT);

        // SQL文を実行
        $m_stmt->execute();

        // 最後に挿入されたIDを取得
        //stock_inputのrot_idへ登録
        $lastInsertId = $dbh->lastInsertId();

        // 各店舗の在庫テーブルにも追加
        $shops = getShops($dbh); // 店舗情報を取得する関数
        foreach($shops as $shop) {
        $s_sql = "INSERT INTO stock (date, code, maker, item, price, basic_quantity, shop_id) 
        VALUES (:date, :code, :maker, :item, :price, :basic_quantity, :shop_id)";    
        $s_stmt = $dbh->prepare($s_sql);
        $s_stmt->bindParam(':date', $date);
        $s_stmt->bindParam(':code', $code);
        $s_stmt->bindParam(':maker', $maker);
        $s_stmt->bindParam(':item', $item);
        $s_stmt->bindParam(':price', $price);
        $s_stmt->bindParam(':basic_quantity', $basic_quantity);
        $s_stmt->bindParam(':shop_id', $shop['shop_id']);

        $s_stmt->execute();
        }
        
    $dbh->commit();

    header('Location:index.php');
    exit;
    } catch (PDOException $e) {
    $dbh->rollback(); // トランザクションロールバック
    echo "Error: " . $e->getMessage();
    
    }

    } else {
        echo "全てのフィールドが入力されていません。";
    }
    
}
?>

商品一覧表示する

//商品一覧表示
$sql = "SELECT
id,
code,
maker,
item,
mail,
basic_quantity,
usage_amount,
price,
capacity,
COALESCE(quantity_per_rot, 0) AS qpr
FROM rot";
$stmt = $dbh->prepare($sql);
$stmt->execute();
$lists = $stmt->fetchAll(PDO::FETCH_ASSOC);

各店舗に登録するための関数を準備

//各店舗に商品マスタと同時に登録するために用意する$shop
function getShops($dbh) {
    $shops = [];
    try {
        $sql = "SELECT * FROM shop"; // `shops`は店舗情報が格納されているテーブル名です。
        foreach ($dbh->query($sql) as $row) {
            $shops[] = [
                'shop_id' => $row['shop_id'],
            ];
        }
    } catch (PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
    return $shops;
}

フォームボタンでポストされたsubmitの確認をしてそれぞれポストされたものを受け取ります。全ての値の存在確認を行い、商品マスタと先ほどのgetShops関数を使って各店舗の在庫情報に新しい商品情報を追加する。

if (isset($_POST['submit'])) {
    
    // 入力された値が存在するかを確認
    $code = isset($_POST['code']) ? $_POST['code'] : null;
    $maker = isset($_POST['maker']) ? $_POST['maker'] : null;
    $item = isset($_POST['item']) ? $_POST['item'] : null;
    $mail = isset($_POST['mail']) ? $_POST['mail'] : null;
    $basic_quantity = isset($_POST['basic_quantity']) ? $_POST['basic_quantity'] : null;
    $usage_amount = isset($_POST['usage_amount']) ? $_POST['usage_amount'] : null;
    $price = isset($_POST['price']) ? $_POST['price'] : null;
    $capacity = isset($_POST['capacity']) ? $_POST['capacity'] : null;
    $quantity_per_rot = isset($_POST['quantity_per_rot']) ? $_POST['quantity_per_rot'] : null;

    // 全ての値が存在するか確認
    if ($code !== null && $maker !== null && $item !== null && $mail !== null && $basic_quantity !== null && $usage_amount !== null && $price !== null && $capacity !== null && $quantity_per_rot !== null) {
        $date = date("Y-m-d");

        try{
            
        $dbh->beginTransaction();
        // 商品マスタに登録
        $m_sql = "INSERT INTO mastar_table (date,code, maker, item, mail, basic_quantity, usage_amount, price, capacity, quantity_per_rot) 
        VALUES (:date,:code, :maker, :item, :mail, :basic_quantity, :usage_amount, :price, :capacity, :quantity_per_rot)";
        
        // PDOオブジェクトを使用してSQL文を実行
        $m_stmt = $dbh->prepare($m_sql);
        
        $m_stmt->bindParam(':date', $date, PDO::PARAM_STR);
        $m_stmt->bindParam(':code', $code, PDO::PARAM_INT);
        $m_stmt->bindParam(':maker', $maker, PDO::PARAM_STR);
        $m_stmt->bindParam(':item', $item, PDO::PARAM_STR);
        $m_stmt->bindParam(':mail', $mail, PDO::PARAM_STR);
        $m_stmt->bindParam(':basic_quantity', $basic_quantity, PDO::PARAM_INT);
        $m_stmt->bindParam(':usage_amount', $usage_amount, PDO::PARAM_STR);
        $m_stmt->bindParam(':price', $price, PDO::PARAM_INT);
        $m_stmt->bindParam(':capacity', $capacity, PDO::PARAM_STR);
        $m_stmt->bindParam(':quantity_per_rot', $quantity_per_rot, PDO::PARAM_INT);

        // SQL文を実行
        $m_stmt->execute();

        // 最後に挿入されたIDを取得
        //stock_inputのrot_idへ登録
        $lastInsertId = $dbh->lastInsertId();

        // 各店舗の在庫テーブルにも追加
        $shops = getShops($dbh); // 店舗情報を取得する関数
        foreach($shops as $shop) {
        $s_sql = "INSERT INTO shops_stock (date, code, maker, item, price, basic_quantity, shop_id) 
        VALUES (:date, :code, :maker, :item, :price, :basic_quantity, :shop_id)";    
        $s_stmt = $dbh->prepare($s_sql);
        $s_stmt->bindParam(':date', $date);
        $s_stmt->bindParam(':code', $code);
        $s_stmt->bindParam(':maker', $maker);
        $s_stmt->bindParam(':item', $item);
        $s_stmt->bindParam(':price', $price);
        $s_stmt->bindParam(':basic_quantity', $basic_quantity);
        $s_stmt->bindParam(':shop_id', $shop['shop_id']);

        $s_stmt->execute();
        }
        
    $dbh->commit();

    header('Location:index.php');
    exit;
    } catch (PDOException $e) {
    $dbh->rollback(); // トランザクションロールバック
    echo "Error: " . $e->getMessage();
    
    }

    } else {
        echo "全てのフィールドが入力されていません。";
    }
    
}

商品マスタの登録と各店舗への登録の間にlastInsertId();でmasterテーブルに最後に登録したidはshop_stockテーブルと関連づける外部キー(商品マスタと各店舗に登録した商品情報を一致させる)としてデータベースに登録できる。

// 最後に挿入されたIDを取得
//stock_inputのrot_idへ登録
$lastInsertId = $dbh->lastInsertId();

HTMLフォームは改めてこんな感じです。

<form action="" method="post">
                      
 <p>メーカー名</p>
 <p><input type="text" name="maker"></p>
                        
 <p>商品コード</p>
 <p><input type="text" name="code"></p>
                        
 <p>商品名</p>
 <p><input type="text" name="item"></p>
                        
 <p>発注ロット</p>
 <p><input type="text" name="quantity_per_rot"></p>
                        
 <p>基本在庫数</p>
 <p><input type="text" name="basic_quantity"></p>
                       
 <p>金額</p>
 <p><input type="text" name="price"></p>
   
 <p>容量</p>
 <p><input type="text" name="capacity"></p>
                       
 <p>1杯使用量</p>
 <p><input type="text" name="usage_amount"></p>
                        
 <p>メール宛先</p>
 <p><input type="text" name="mail"></p>
                       
 <input type="submit" name="submit" value="保存" class="ma_top_2">
                        
 </form>
                  
              <table >
                  <tbody>
                    <tr>
                      <th>メーカー名</th>
                      <th>商品コード</th>
                      <th>商品名</th>
                      <th>発注ロット</th>
                      <th>基本在庫数</th>
                      <th>金額</th>       
                      <th>容量</th>  
                      <th>1杯使用量</th>
                      <th>メール宛先</th>
                      
                    </tr>
                    <?php foreach($lists as $list){ ?>
                    <tr>
                      <td><?php echo $list['maker'];?></td>
                      <td><?php echo $list['code'];?></td>
                      <td><?php echo $list['item']; ?></td>
                      <td><?php echo $list['qpr'];?></td>
                      <td><?php echo $list['basic_quantity']; ?></td>
                      <td>¥<?php echo number_format($list['price']);?></td>
                      <td><?php echo $list['capacity'];?></td>
                      <td><?php echo $list['usage_amount']; ?></td>
                      <td><?php echo $list['mail'];?></td>
                      <td><a href="edit.php?id=<?php echo $list['id']; ?>" class="h_detail de">編集</a></td>
                      <form action="delete.php" method="POST">
                        <input type="hidden" name="id" value="<?php echo $list['id'];?>">
                      <td><button type="submit" class="h_detail">削除</button></td>
                    </form>
                    </tr>
                    <?php } ?>
                    
                  </tbody>
                </table>

フォーム部分で商品情報を入力を行います。

<form action="" method="post">
                      
                          <p>メーカー名</p>
                          <p><input type="text" name="maker"></p>
                        
                          <p>商品コード</p>
                          <p><input type="text" name="code"></p>
                        
                          <p>商品名</p>
                          <p><input type="text" name="item"></p>
                        
                          <p>発注ロット</p>
                          <p><input type="text" name="quantity_per_rot"></p>
                        
                          <p>基本在庫数</p>
                          <p><input type="text" name="basic_quantity"></p>
                       
                          <p>金額</p>
                          <p><input type="text" name="price"></p>
   
                          <p>容量</p>
                          <p><input type="text" name="capacity"></p>
                       
                          <p>1杯使用量</p>
                          <p><input type="text" name="usage_amount"></p>
                        
                          <p>メール宛先</p>
                          <p><input type="text" name="mail"></p>
                       
                       
                        <input type="submit" name="submit" value="保存" class="ma_top_2">
                        
                    </form>

登録されたものは以下のように商品の項目と繰り返し構文で表示させます。

<table >
                  <tbody>
                    <tr>
                      <th>メーカー名</th>
                      <th>商品コード</th>
                      <th>商品名</th>
                      <th>発注ロット</th>
                      <th>基本在庫数</th>
                      <th>金額</th>       
                      <th>容量</th>  
                      <th>1杯使用量</th>
                      <th>メール宛先</th>
                      
                    </tr>
                    <?php foreach($lists as $list){ ?>
                    <tr>
                      <td><?php echo $list['maker'];?></td>
                      <td><?php echo $list['code'];?></td>
                      <td><?php echo $list['item']; ?></td>
                      <td><?php echo $list['qpr'];?></td>
                      <td><?php echo $list['basic_quantity']; ?></td>
                      <td>¥<?php echo number_format($list['price']);?></td>
                      <td><?php echo $list['capacity'];?></td>
                      <td><?php echo $list['usage_amount']; ?></td>
                      <td><?php echo $list['mail'];?></td>
                      <td><a href="edit.php?id=<?php echo $list['id']; ?>" class="h_detail de">編集</a></td>
                      <form action="delete.php" method="POST">
                        <input type="hidden" name="id" value="<?php echo $list['id'];?>">
                      <td><button type="submit" class="h_detail">削除</button></td>
                    </form>
                    </tr>
                    <?php } ?>
                    
                  </tbody>
                </table>