2016-04-28 36 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