2012-02-22 72 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