2011-07-10 72 views
0

我有一個插入函數,我使用MySQL real_escape_string()刪除非法字符只有它並沒有實際刪除這些字符,任何人都可以看到我要去哪裏錯了?MySQL真正的轉義字符串

$interest = mysql_real_escape_string(urldecode($_GET['interest'])); 

    $query = "INSERT INTO user_interests (user_id, interest) VALUES('{$user_id}' , '{$interest}')"; 
    mysql_query($query) or die(mysql_error()); 
    echo $interest; 
+2

因爲mysql_real_escape_string不會刪除字符,所以當它們發送到數據庫時,它會將查詢引號轉義。請務必在使用此功能之前建立連接,否則將返回FALSE –

+0

ahh thankyou ... – Liam

+0

您想要做什麼?你想要刪除什麼字符? –

回答

2

沒有「非法字符」。 mysql_real_escape_string只是對所有字符進行編碼,以便它們可以安全地放入查詢中。如果你想刪除一個字符c,使用str_replace

$input = urldecode($_GET['interest']); 
$input = str_replace('c', '', $input); 
$interest = mysql_real_escape_string($input); 
2

mysql_real_escape_string只是逃脫你的字符串中的字符,當您試圖將它們寫入到數據庫可能會引發問題。這並不意味着它會將其刪除。

想象一下,您正在接受用戶輸入並且用戶在輸入字段中輸入了一個引號。當您嘗試將該字符串插入數據庫時​​,該引號將被解釋爲sql查詢中的引號,並且該查詢將無法正常工作。

INSERT INTO table (string) 
VALUES ("this is a string with an extra " in it") 

如果你第一次在這個字符串中使用mysql_real_escape_string,那麼你的SQL查詢將基本上是這樣的:

INSERT INTO table (string) 
VALUES ("this is a string with an extra \" in it") 

見上逃逸反斜線。你可以看到這個額外的引用,甚至在SO上混淆了格式。