2012-02-22 69 views
0

當我從網絡瀏覽器訪問它時,它只會返回echo'd文本,我知道這與我發佈的另一個問題類似,但我無法理解它?Web瀏覽器沒有任何返回 - 函數錯誤?

<?php 
include('config.php'); 
include('database.php'); 

    class conversion{ 

public $amnt; 
public $cc_from; 
public $cc_to; 

public function __construct(){ 
    $this->amnt = htmlspecialchars($_GET["amnt"]); 
    $this->cc_from = htmlspecialchars($_GET["from"]); 
    $this->cc_to = htmlspecialchars($_GET["to"]); 
    } 

function convert($this->amnt,$this->cc_from,$this-cc_to,$decimals=2){ 
$db_rate_from = mysql_query("SELECT * FROM _currency WHERE country_code='$this- >cc_from'") or die(mysql_error());; 
$query_row_from = mysql_fetch_array($db_rate_from); 
$rate_from = ($query_row_from['rate']); 
echo $rate_from; 
echo "</br>rate to</br>"; 

$db_rate_to = mysql_query("SELECT * FROM _currency WHERE country_code='$this->cc_to'")  or die(mysql_error());; 
$query_row_to = mysql_fetch_array($db_rate_to); 
$rate_to = ($query_row_to['rate']); 
echo $rate_to; 
echo "</br>conversion</>"; 

$conversion = (number_format(($amnt/$rate_from)*$rate_to,$decimals)); 
echo $conversion; 

} } 
$var = new conversion(); 
$var->convert($amnt,$cc_from,$cc_to); 
?> 
+2

好的SQL注入漏洞。使用htmlspecialchars來「安全」SQL數據就像使用衛生紙廣場清理海洋。 – 2012-02-22 14:55:00

+1

縮進你的代碼可以提供幫助,僅僅在FYI之後。 – Ryan 2012-02-22 14:55:15

+3

您的預期產出是多少?您的實際產出是多少? – 2012-02-22 14:55:52

回答

3

有鑑於此:

$db_rate_from = mysql_query("SELECT * FROM $db_tbprefix WHERE country_code='$this->cc_from'"); 

其中$db_tbprefix定義?無處不在,導致您的查詢爲SELECT * FROM WHERE ...。如果你有適當的SQL錯誤處理代碼,這將會很清楚。在絕對最低限度,你應該是這樣的:

$result = mysql_query("...") or die(mysql_error()); 

這將中止對查詢失敗腳本,並確切地告訴你爲什麼查詢失敗。

同時,用htmlspecialchars不用於數據庫操作。它絕對不會阻止SQL注入。爲此,您必須使用mysql_real_escape_string()

+0

我的錯誤mysql_error()在我的配置文件中,所以它應該帶來肯定? – 2012-02-22 15:00:29

+2

不必檢查每個mysql_query(),mysql_connect )等等......打電話給你執行 – 2012-02-22 15:01:50

+0

一直以來都不是直接使用mysql_query,而是使用自己的my_own_query(或其他),它總是執行mysql_query('...')或my_error_handling_function( ); – 2012-02-22 15:06:18

1

避風港」測試...但我能找到的可能性是要傳遞參數函數轉換而定義,所以你需要通過同PARAM同時調用它...或者,如果變量是從參考預定義的一個然後像這樣使用它

function convert($this->amnt,$this->cc_from,$this->cc_to,$decimals=2){ 
    } 
+0

我覺得'$ this->'部分不應該在參數列表:) – 2012-02-22 15:04:28

2

我注意到的一件事是,你調用你的方法沒有參數。

$var->convert(); 

然而,它被宣佈採取三個強制參數。

function convert($amnt,$cc_from,$cc_to,$decimals=2) 

順便說一句,不要使用$ query_row_to [rate]。使用$ query_row_to ['rate']或$ query_row_to [$ rate]。

編輯:

這樣的事情呢?使用全局$ db_tbprefix並跳過對象方向。

<?php 
include('config.php'); 
include('database.php'); 

    function convert($amnt,$cc_from,$cc_to,$decimals=2) { 
     global $db_tbprefix; 
     $db_rate_from = mysql_query("SELECT rate FROM $db_tbprefix WHERE country_code='$cc_from'") or die mysql_error(); 
     $query_row_from = mysql_fetch_assoc($db_rate_from); 
     $rate_from = $query_row_from['rate']; 

     $db_rate_to = mysql_query("SELECT rate FROM $db_tbprefix WHERE country_code='$cc_to'") or die mysql_error(); 
     $query_row_to = mysql_fetch_assoc($db_rate_to); 
     $rate_to = $query_row_to['rate']; 

     return number_format(($amnt/$rate_from)*$rate_to,$decimals); 
    } 


echo convert(floatval($_GET["amnt"]), mysql_real_escape_string($_GET["from"]), mysql_real_escape_string($_GET["to"])); 
?> 

編輯2:只選擇你需要的,在這種情況下,率。並且使用mysql_fetch_assoc而不是使用mysql_fetch_array,這會使內存消耗加倍並減慢代碼。

+0

謝謝 - 我已經把$ var-> convert($ amnt,$ cc_from,$ cc_to);相反,但netbeans是說他們未初始化? – 2012-02-22 15:09:47

+0

那是因爲他們。你必須全局初始化它們,或者你需要改變你的方法來跳過這些參數,並使用$ this-> amnt,$ this-> cc_from,$ this-> cc_to來代替。 – 2012-02-22 15:14:40

+0

我是否需要讓他們在課外?在課程開始時,他們在那裏 – 2012-02-22 15:17:51