PHP获取值以后如何格式化

WBOY
Release: 2016-06-13 12:18:51
Original
930 people have browsed it

PHP获取值以后怎么格式化
数据库里面有一个表,两个字段

id int (5) 

content    text  

然后 form 的表单是这样的

$content=$_POST['content'];
$sql="insert into dbname (`id`,``) values(null,'$content')":





Copy after login


现在的问题是如果我在这个表单里面如果写很多类似符号,比如单引号,双引号,或者!@#$$%这样的东西就会提交失败,我吧sql打印出来插入也会失败,应该是起来冲突了,因为我在插入的值的时候用了单引号, 如果提交的值也有单引号,应该就是不知道


------解决思路----------------------
 插入前先轉義
只需要轉一次
$content = mysql_escape_string($_POST['content']);
$sql = "insert into dbname (`id`,`content`) values(null,'$content')":

參考:http://php.net/manual/zh/function.mysql-escape-string.php

------解决思路----------------------
楼上已经解答完了
这里普及一下:
              mysql_real_escape_string 主要是为了 数据库防注入、以及语句正确性等需要,将读写语句中的字符进行了转换;
但最终写入到数据库中的内容,依旧是你转义前的;所以当你读出来的时候,依旧是原来转义前的内容;
比如,你要在数据库中插入字符串 aaaa"aaaa 但如果写成insert into table values("aaaa"aaaa"); 语句就出错了,使用mysql_real_escape_string就能转义成 aaaa\"aaaa; 最终语句就变成了 insert into table values("aaaa\"aaaa");  
但你select * from table 最终得到的内容依旧是 aaaa"aaaa

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!