2013-01-13 98 views
0

您好我有下面的語句需要幫助防止SQL注入

function count_rows($table_name, $condition = null, $debug = false) 
{ 
    $query_result = $this->query("SELECT count(*) AS count_rows FROM " . $this->db_prefix . $table_name . " " . $condition, $debug); 
    $count_rows = $this->sql_result($query_result, 0, 'count_rows'); 

    return $count_rows; 
} 

我一直在使用SQL注入我插件的Firefox,它給我的錯誤,同時運行發生

一個MySQL錯誤腳本:

The query you are trying to run is invalid 
Mysql Error Output: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 2 
SQL Query: SELECT count(*) AS count_rows FROM database_auctions a WHERE a.active=1 AND a.approved=1 AND a.deleted=0 AND a.list_in!='store' AND a.catfeat='1' AND a.closed=0 AND (a.category_id IN()) 

如何清理此查詢針對sql注入?

+0

清理無效查詢嗎?我不明白。 – 2013-01-13 23:20:05

+0

嗨,大家好,讓我解釋一下,我使用插件sql注入了我並在發佈字段'OR用戶名IS NOT NULL或用戶名='後得到了錯誤,它通常不是無效的查詢並且工作正常 – user1973125

回答

1

正如你可以看到靠近你查詢的這個部分:

AND (a.category_id IN())

其實你不給它的值的子查詢/列表。您需要這樣做來確定結果是否保存指定的category_id。

爲了防止sql注入,我建議使用PHP的mysqli擴展。我相信他們支持準備好的陳述。通過使用預準備語句,您將放棄使用字符串連接,並且服務器「準備」sql語句,因此查詢只發送給服務器一次,然後在您實際執行SQL查詢時僅發送參數。

http://php.net/manual/en/class.mysqli-stmt.php

http://php.net/manual/en/mysqli-stmt.prepare.php

1

看起來,當你在這裏a.category_id IN()通過parentesis之間的$contidion你忘了什麼東西應該是值等。 爲避免SQL注入檢查this