多个post数组循环不起作用

<?php
    $sql = "SELECT * FROM product ORDER BY product_id DESC";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {

            echo "" . $row["product"] . "

<input type='checkbox' name='product[]' value=" .$row["product"] . ">
         
     Quantity

<select name='quantity[]'>
        <option value='1'>1</option>
        <option value='2'>2</option>
        <option value='3'>3</option>
    </select><br />";
        }

    ?>
    <button name="submit" type="submit">submit</button><br />

    <?php

        if (isset($_POST['submit'])) {
            if (is_array($_POST['product'])) {
                foreach ($_POST['product'] as $key => $product_name) {
                    $quantity = $_POST['quantity'][$key];
                    echo $product_name . "
";
                    echo $quantity . "<br>";

                }
            }
        }
    }

    ?>

i would want to select multiple product and quantity but it doesn't giving me quantity value what i select.

example picture1
on example picture1 you could see i selected 2 products

  • Asus 2
  • Acer 3

But if click on submit i get result like
example picture2

  • Asus 1
  • Acer 1

Try using the product ID to link the checkboxed to the select boxes.:

$sql = "SELECT * FROM product ORDER BY product_id DESC";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {

            echo "" . $row["product"] . "

<input type='checkbox' name='product[".$row['product_id']."]' value=" .$row["product"] . ">
         
     Quantity

<select name='quantity[".$row['product_id']."]'>
        <option value='1'>1</option>
        <option value='2'>2</option>
        <option value='3'>3</option>
    </select><br />";
        }