【问题标题】:Check $_POST['id'] is numeric then do this if not do otherwise检查 $_POST['id'] 是否为数字,否则执行此操作
【发布时间】:2012-11-19 12:24:35
【问题描述】:

您好,我的代码似乎失败了:

if (!empty($_POST['id'])) { 
    echo "empty";
} else {
    if (is_numeric($_POST['id'])) {
    echo "numeric!";
    } else {
    echo "not empty but not numeric how come?";
    }
}

我的浏览器网址:hxxp://localhost/upload/?id=9

输出:不是数字

怎么来的?

请帮忙。

【问题讨论】:

    标签: php post isnumeric


    【解决方案1】:

    应该使用if(is_numeric($_GET['id'])) {


    if (is_numeric($_GET['id'])) { 
        echo "yes numeric";
    } else {
        echo "not numeric";
    }
    

    【讨论】:

      【解决方案2】:

      第一:

      if (!empty($_POST['id'])) { 
          echo "empty";
      } else ...
      

      您是说:如果变量不为空,则回显“空”,然后您正在检查空变量是否为数字(else 中的代码正在检查空变量,这就是为什么它说它是不是数字)

      取出感叹号,并澄清自己使用 post 或 get 方法,因为当您通过 GET 传递它时尝试获取 POST 变量

      【讨论】:

        【解决方案3】:

        看到这个问题:$_POST Number and Character checker

        // test.php
        
        // testing with $_POST['id'] from forum with id = 5 and another test where id = new
        
        $id = $_POST['editid'] ;
        echo "<br>---".$id."---<br>";
        
        if (empty($id)) { 
        echo "<br>1: empty";
        } else {
        if (!is_numeric($id)) {
        echo "<br>2: This is the number 5";
        } else {
        echo "<br>3: the must be the word new";
        }
        }
        
         // test 2 ... ctype_digit
        
        
        if (empty($id)) { 
        echo "<br>4: empty";
        } else {
        if (!ctype_digit($id)) {
        echo "<br>5: This is the number 5";
        } else {
        echo "<br>6: the must be the word new";
        }
        }
        
        // test 3 ... 
        
        
        
        if (empty($id)) { 
        echo "<br>7: empty";
        } else {
        if (!preg_match('#[^0-9]#',$id)) {
        echo "<br>8: This is the number 5";
        } else {
        echo "<br>9: the must be the word new";
        }
        }
        
        /**
        
        result from 5
        
        
        ---5---
        
        3: the must be the word new
        6: the must be the word new
        8: This is the number 5
        
        results from "new"
        
        
        
        **/
        

        【讨论】:

        • ---new--- 2:这是数字 5 5:这是数字 5 9:必须是单词 new
        • ---new--- 2:这是数字 5 5:这是数字 5 9:必须是单词 new
        【解决方案4】:

        我认为你是通过 URL 传递参数所以使用

        if (is_numeric($_GET['id']))
        

        或者使用

        if (is_numeric($_REQUEST['id'])) { 
        

        否则它将显示一个未定义的变量,因此将回退到每个块

        【讨论】:

          【解决方案5】:

          很简单,“id”在 $_GET 数组中,但您检查 $_POST 数组中的存在

          if (empty($_GET['id'])) { ... }
          

          应该是正确的。然后你可以使用 $_GET['id'] 或 $_REQUEST['id']。

          注意:$_REQUEST 包含 $_POST 和 $_GET 中的所有变量

          正确的代码应该是:

           if (empty($_GET['id'])) { 
               echo "empty";
           } else {
               if (is_numeric($_GET['id'])) {
                   echo "numeric!";
               } else {
                   echo "not empty but not numeric how come?";
               }
           }
          

          你也可以使用 $_REQUEST 来代替 $_GET

          【讨论】:

            猜你喜欢
            • 2021-02-10
            • 1970-01-01
            • 2023-03-09
            • 2013-01-19
            • 2021-10-27
            • 2011-08-01
            • 2014-02-17
            相关资源
            最近更新 更多