函数中的IF语句的问题

时间:2011-05-03 19:05:53

标签: php

我是PHP程序员的新手,我创建了一个更改SQL命令的函数。这是相关的代码:

 $extra_text_length = strlen($_GET[extra_text]);
 $boolean = $_GET['first-boolean'];

 function check_boolean(){

  if($extra_text_length > 0){

    if($boolean=="and"){
        $query .= " AND (software.SWID='$_GET[extra_text]')";
        } elseif($boolean=="or"){
        $query .= " OR (software.SWID='$_GET[extra_text]')";
        } elseif($boolean=="not"){
        $query .= " NOT IN (software.SWID='$_GET[extra_text]')";
        }
    } return $boolean;
  }

check_boolean();

问题在于它没有做它应该做的事情。但是,如果我从函数中删除代码,结果我删除了check_boolean()方法,它完美地工作。有人会给我一个提示吗?提前致谢

7 个答案:

答案 0 :(得分:3)

boolean缺少$,但我更愿意将其设为function check_boolean($bool)之类的参数,而不是使用全局变量。另外,请阅读SQL injection 。这是你现在的一个可怕的漏洞。最短的解决方案是将mysql_real_escape_string()放在查询中的变量周围(假设您正在使用MySQL),但请详细了解SQL注入。

答案 1 :(得分:1)

问题是变量的范围,当你在功能块中输入时,全局范围内的变量是不可见的。

您应该将它们作为参数传递。你的电话应该是:

check_boolean($extra_text_length, $boolean, $software);

你要将你的函数声明更改为:

function check_boolean($extra_text_length, $boolean, $software)

此外,最好避免混合全局代码和函数。你可以将它们放在一个单独的文件中,并使用require_once来包含它们。

我还鼓励你根本不使用任何全局代码。例如,您可以将前两行包装在main()函数中。

同样“return boolean;”应该是“return $ query”。

答案 2 :(得分:1)

确保使用变量名称$ boolean而不是boolean,否则它会尝试获取常量。

您还必须小心传递给方法的内容。 $ query是您尚未定义的变量。

确保做到这样的事情

 $extra_text_length = strlen($_GET[extra_text]);
 $boolean = $_GET['first-boolean'];

 function check_boolean($query, $extra_text_length, $boolean, $software){

  if($extra_text_length > 0){

    if($boolean=="and"){
        $query .= " AND ($software.SWID='$_GET[extra_text]')";
        } elseif($boolean=="or"){
        $query .= " OR ($software.SWID='$_GET[extra_text]')";
        } elseif($boolean=="not"){
        $query .= " NOT IN ($software.SWID='$_GET[extra_text]')";
        }
    }
    return $query;
  }

$query = '...';
$software = '';
$query = check_boolean($query, $extra_text_length, $boolean, $software);

此外,避免在函数中使用外部变量,因为您不知道它们将如何反应。 ($ _ GET) 您还应检查所有变量是否存在。

答案 3 :(得分:1)

首先,您必须在$boolean表达式中使用boolean而不是if

第二:$query$extra_text_length$boolean必须在check_boolean内可见。您可以将它们作为参数传递,例如:

function check_bookean(&$query, $extra_text_length, $boolean) {
    # ...
}

check_boolean($query, $extra_text_length, $boolean);

$query需要在函数定义中以&符号(&)作为前缀,因为它在函数内被修改。

然后

check_boolean将根据extra_text对您进行查询并为其添加布尔条件。但是在将内容包含在查询中之前需要清理内容,以避免SQL注入:

if($extra_text_length > 0){

    $extra_text = mysql_real_escape_string($_GET['extra_text']);  # sanitize content

    if(boolean=="and"){
        $query .= " AND (software.SWID='$extra_text')";           # to use it in the query
        } elseif(boolean=="or"){
        $query .= " OR (software.SWID='$extra_text')";
        } elseif(boolean=="not"){
        $query .= " NOT IN (software.SWID='$extra_text')";
        }
    } return boolean;
}

答案 4 :(得分:0)

你在$ boolean之前忘记了$符号。当我开始使用PHP了解其他语言时,我做了同样的事情; - )。

答案 5 :(得分:0)

boolean ==“和”应该是$ boolean ==“和”

答案 6 :(得分:0)

$extra_text_length$boolean$query在函数内部不可用。你必须将它们作为参数传递。

此外,更改函数内部的参数不会更改其在函数外部的值。返回文本。这是一个稍微改进的版本:

function check_boolean($length, $b, $extra_text){
  $operators = array('and', 'or', 'not');
  if($length > 0){
     if(in_array($b, $operators){
         return " " . $b . " (software.SWID='" . $extra_text . "')";
     }
  }
  return '';
}

$query .= check_boolean($extra_text_length, $boolean, mysql_real_escape_string($_GET['extra_text']));

请注意,您必须在用户输入上调用mysql_real_escape_string,否则您的代码很容易被SQL注入。

相关问题