2011-05-03 45 views
0

我是新手PHP程序員,我創建了一個更改SQL命令的函數。這是相關的一段代碼:函數中的IF語句的問題

$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()方法,它完美地工作。有人會給我一個提示嗎?在此先感謝

回答

1

確保使用變量名$ 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) 您還應該檢查所有變量的存在。

0

您忘記$布爾值之前的$符號。當我開始使用PHP知道其他語言時,我也做了同樣的事情;-)。

3

boolean是缺少$,但我寧願使它像function check_boolean($bool)而不是使用全局變量的參數。另外,請閱讀SQL injection。這是你現在的一個可怕的漏洞。最短的解決方案是在查詢中放置mysql_real_escape_string()(假設您正在使用MySQL),但請徹底瞭解SQL注入。

0

布爾==「和」應該是$布爾==「和」

1

那麼問題是變量的範圍,當你在功能塊進入,在全球範圍內的變量是不可見。

您應該將它們作爲參數傳遞。您的電話應該是:

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

,你已經改變你的函數聲明爲:

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

而且,它會更好,切忌混全球代碼和功能。你可以把它們放在一個單獨的文件中並使用require_once來包含它們。

我也鼓勵你不要使用任何全局代碼。例如,您可以將前兩行包裝在main()函數中。

另外「return boolean;」應該是「返回$查詢;」

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注入。

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; 
}