duanao4729 2014-01-25 23:36
浏览 25
已采纳

为什么我的foreach输入未设置?

Hey, I'm a novice at PHP, so have mercy on me!

I'm trying to echo $qty, but it's saying that the variable is an undefined variable.

If you see anything I'm doing wrong, feel free to tell me!

Thanks in advance!

action.php:

            foreach ($rows as $row){
              $food = $row['food'];
              $price = $row['price'];
              if(isset($_POST['qty'])){
                $qty = $_POST['qty'];
                echo 'set';
              }else{
                  echo 'unset';

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

order.php:

            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' placeholder='#' name='qty' maxlength='1'></td>
                </tr>";
        }
  • 写回答

3条回答 默认 最新

  • duanjue9889 2014-01-25 23:37
    关注

    It seems almost certain then that if(isset($_POST['qty'])){ didn't pass, so the variable wasn't given a value. Make sure you are passing POST data to this script

    If I am correct, you should see the word "unset" in the output.

    You could set $qty in advance so that no matter what, it will have a value, or give it a different value if your if statement is false:

    //set a default value earlier
    $qty = 'This is the default value!';
    
    //or in the if statement
    if(isset($_POST['qty'])){
      $qty = $_POST['qty'];
      echo 'set';
    } else {
      $qty = 'qty wasn't given a value!';
      echo 'unset';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了