PHP Isset问题 [英] PHP Isset Issue

查看:54
本文介绍了PHP Isset问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用isset()检查变量是否具有值.我的问题是,即使变量没有值(参数未在url中传递),sql字符串仍会修改为追加where子句.

I am using isset() to check if a variable has a value. My issue is that even when the variable has no value (the parameter was not passed in the url) the sql string is still modified to append the where clause.

例如-如果这是传递的URL

For example - if this is the URL that is passed

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'

但是,此URL http://internal/test/trbm会产生以下回显结果,并且仍然附加where子句,即使(至少对我来说)isset()应该返回false

However, this URL http://internal/test/trbm produces the below echo results, still appending the where clause, even though (at least to me) isset() should return false

Select * from users where userID = ''

我期望/期望的第二个echo语句(上面的一个)是

The second echo statement (the one right above) my desired/expected to be is

Select * from users

isset()是在此实例中使用的错误检查吗?如果是这样,我应该在该位置使用什么支票?

Was isset() the incorrect check to use in this instance? If so, what check should I be using in it's place?

推荐答案

$user将被设置,因为您在此行中对其进行了设置

$user will be set, because you set it in this line

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

因此,直接检查$_GET值.

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

echo $sql;

这篇关于PHP Isset问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆