为什么$ _POST为空?使用变量$ row作为输入名称使用foreach

时间:2014-01-25 21:32:16

标签: php mysql

嘿,我一直试图解决这个问题几个小时了。我对PHP很新,所以对我有一些同情!

我正在尝试调用$ _POST [$ id],并使用foreach将其放入表中。它也告诉我id是一个未识别的索引。知道为什么吗?如果您对初学者有任何其他指示,请随时分享

提前谢谢!

索引表:

  foreach ($rows as $row) {
            $food = $row["food"];
            $price = $row["price"];
            $picture = $row["picture"];
            $description = $row["description"];
            $id = $row['id'];

            echo "<tr>
                    <td><img src='$picture' width='120px' /></td>
                    <td>$food</td>
                    <td>$$price</td>
                    <td><input type='number' min='0' max='10' enctype='multipart/form-data' placeholder='#' name='<?php echo $id; ?>' maxlength='1'></td>
                </tr>";
        }

submitorder.php表:

 while($row = $result->fetch_array()){
            $rows[] = $row;
        }

        foreach ($rows as $row) {
            $food = $row["food"];
            $price = $row["price"];

            $id = $_POST[$id];
            if(!empty($_POST[$id])){
                if(isset($_POST[$id])){
                    $qty = $_POST[$id];
                }else{
                    echo "Is NOT SET";
                }
            }else{
                echo "Is EMPTY";
            }

            echo "<tr>
                    <td>$food</td>
                    <td></td>
                    <td>$$price</td>
                </tr>";
        }

2 个答案:

答案 0 :(得分:1)

foreach ($rows as $row) {
            $food = $row["food"];
            $price = $row["price"];
            $picture = $row["picture"];
            $description = $row["description"];
            $id = $row['id'];

            echo '<tr>
                    <td><img src='.$picture.' width=\'120px\' /></td>
                    <td>'.$food.'</td>
                    <td>'.$price.'</td>
                    <td><input type=\'number\' min=\'0\' max=\'10\' enctype=\'multipart/form-data\' placeholder=\'#\' name='.$id.' maxlength=\'1\'></td>
                </tr>';
        }



while($row = $result->fetch_array()){
                $rows[] = $row;
            }

            foreach ($rows as $row) {
                $food = $row["food"];
                $price = $row["price"];
                   if(isset($_POST['id'])){
                        $qty = $_POST['id'];
                    }else{
                        echo "Is NOT SET";
                    }
                echo '<tr>
                        <td>' . $food . '</td>
                        <td></td>
                        <td>' .$price. '</td>
                    </tr>';
            }

修复错误,我唯一能想到的是你的表单实际上并没有使用POST作为方法。此外,为了您自己和他人,如果$ _POST ['id']值实际上是数量,请将其重命名为数量,以免给自己带来一些麻烦。

答案 1 :(得分:1)

问题在于:

<td><input type='number' min='0' max='10' enctype='multipart/form-data' placeholder='#' name='<?php echo $id; ?>' maxlength='1'></td>

具体来说,这里:

.... name='<?php echo $id; ?>' ....

您已经在PHP字符串中。你应该连接起来。否则,您的元素名称实际上是<?php echo [whatever the ID is]; ?>