代码之家  ›  专栏  ›  技术社区  ›  BellHopByDayAmetuerCoderByNigh

PHP Isset问题

  •  1
  • BellHopByDayAmetuerCoderByNigh  · 技术社区  · 8 年前

    我正在使用 isset()

    http://internal/test/trbm?userID=3213
    

    这是我的php代码片段

    $user = urldecode($_GET['userID']);
    
    $sql = "Select * from users"
    if (isset($user)) {
        $sql .= " WHERE userID = '$user'";
    }
    
    echo $sql;
    

    回波结果为:

    Select * from users where userID = '3213'
    

    http://internal/test/trbm 生成以下回显结果,仍然附加where子句,即使(至少对我来说) isset() 应返回false

    Select * from users where userID = ''
    

    第二个echo语句(正上方的一个)是我想要/期望的

    Select * from users
    

    isset() 在这种情况下使用的错误检查?如果是这样,我应该在它的位置使用什么支票?

    5 回复  |  直到 8 年前
        1
  •  2
  •   RiggsFolly    8 年前

    $user

    $user = urldecode($_GET['userID']);
    

    因此,请检查 $_GET 直接价值。

    $sql = "Select * from users"
    if (isset($_GET['userID']) {
        $user = urldecode($_GET['userID']);
        $sql .= " WHERE userID = '$user'";
    }
    
    echo $sql;
    
        2
  •  2
  •   Lodder    8 年前

    适当的 还有,用Joomla的 JInput

    $userId = JFactory::getApplication()->input->get('userID', '', 'STRING');
    $sql    = 'Select * from users';
    
    if (isset($userId)
    {
        $sql .= ' WHERE userID = ' . urldecode($userId);
    }
    
    echo $sql;
    
        3
  •  1
  •   Kevin P    8 年前

    运行isset()检查是否已提交$\u GET var,如果已提交,请将$user变量设置为它,否则保留为空

    $user = '';
    if (isset($_GET['userID'])) {
        $user = urldecode($_GET['userID']);
    }
    
    $sql = "SELECT * FROM users";
    
    if ($user != '') {
    
        $sql .= " WHERE userID = '$user'";
    
    }
    
    echo $sql;
    
        4
  •  1
  •   GrumpyCrouton    8 年前

    $user $_GET 变量在IF语句中设置,并设置 $用户 IF语句中的变量。

    empty() 函数-即假设变量无论如何都不应该为空。

    如果变量 必须等于某物

    bool empty ( mixed $var )

    以下是被视为空白的内容列表:

    • “”(空字符串)
    • 0(0为整数)
    • 0.0(0作为浮点)
    • 无效的
    • array()(空数组)
    • $var;(声明了一个变量,但没有值)

    您可以在以下网站了解更多信息: php.net

    您需要检查变量是否为 empty 在将其设置为页面变量之前。

    if(!empty($_GET['userID'])) {
        $user = $_GET['userID'];
        $sql .= " WHERE userID = '$user'";
    }
    

    你可以放一个 else


    当然,你也可以用isset做同样的事情,但它检查的东西更少。

    空的 您需要NOT运算符 ! 但对于isset,你不希望这样。),因为您想确保它设置为设置变量。

    if(isset($_GET['userID'])) {
        $user = $_GET['userID'];
        $sql .= " WHERE userID = '$user'";
    }
    

    其他的

        5
  •  0
  •   Sunil K Samanta    8 年前

    你可以简单地使用这个

    if($_GET['userID']) {
        $user = $_GET['userID'];
        $sql .= " WHERE userID = '$user'";
    }
    
    推荐文章