2016-04-28 58 views
0

在PHP從PHP特殊字符不被插入到MySQL

<input type="checkbox" name="CBage" id="cbage2" value="and age >= 18 and age <= 24" checked/><label for="cbage2">18-24</label> 

值= 「和年齡> = 18和年齡< = 24」

在PHP後動作(我已經嘗試)後

$age1 = $_POST['CBage']; 
$age = mysql_real_escape_string(implode(",", $age1)); 

$age = implode(",", $age1); 

表作爲

CREATE TABLE `jobs` (
`age` varchar(200) COLLATE utf8_unicode_ci 
)ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

在MySQL創建它插入作爲

INSERT INTO jobs VALUES ('$age') 

插入作爲該值如下

and age = 18 and age,and age = 25 and age, 

的問題是,它不正確插入的值(其缺少> <和一些文字在最後)

"and age >= 18 and age <= 24" but rather it inserted as "and age = 18 and age,and age = 25 and age," 
+0

的(http://stackoverflow.com/questions/2843849/mysql-real-escape-more-than-once)嘗試使用ヶ輛() –

+0

沒有它不是」可能重複t錯過了那些角色;你看着網頁瀏覽器中的值,它將它們視爲HTML標記中的特殊字符....做一個「查看源代碼」來查看數據庫中真正檢索的內容 –

+0

確定首先,使用mysql_代替使用PDO或MYSQLI。嘗試粘貼一個完整的代碼,以幫助更好地瞭解您的問題 –

回答

0

嘗試使用ヶ輛會照顧所有特殊charecters「<」(小於)成爲「<」「>」(大於)成爲「>」然後提取使用html_entity_decode當扭轉它

'<' (less than) becomes '&lt;' '>' (greater than) becomes '&gt;'然後取出用html_entity_decode時

而且其重要停止使用Mysql_以避免嚴重的SQL注入的危險 使用PDO或mysqli的編制報表扭轉它

$var1=htmlentities ($_POST['var1']) ; 


$sth = $dbh->prepare('INSERT INTO table(field1) VALUES (?)'); 

    $sth->bindParam(1, $var1, PDO::PARAM_STR); 
    $sth->execute(); 

去取回來

$sth = $dbh->query('SELECT * FROM table'); 

while($row = $sth ->fetch(PDO::FETCH_ASSOC)) { 
    echo html_entity_decode($row['field1']); //etc... 
} 
+1

更新了錯字 –

+0

感謝您的建議。 – Newbie2Bootstrap