douza1373 2016-11-25 22:49
浏览 44
已采纳

多个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
  • 写回答

1条回答 默认 最新

  • doumei1908 2016-11-25 23:04
    关注

    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 />";
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?