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

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题