PHP某些特殊字符无法插入数据库! 经过测试,"" '' // ?? 《》<>《》:;‘”,。、?!@#¥%……&×()!@!@等特殊符号能存入数据库也能取出。#¥%……&*()和#$%^&*()无法存入数据库,这是啥原因啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你用的是什么数据库?起码不是 mysql 是mysql...我刚才直接去写sql语句发现可以插#但是插'的确不行,单数的'都会有错,双数的'才行,要加/才行。另外纠正下我插#的时候不是插不进去,是插进去一个空的。 我在php里给变量都加了check,用了addslashes转义,现在'可以插入了现在不能插入数据库的是#和&,数据库里取到的是空值。肯定是因为URL的问题。大家有谁知道什么原因么? JS里用到URL的地方。这里我输入#那么content=#//alert(content); var url = 'insert.php?author='+author+ '&imgurl=' +imgurl+ '&content='+content; //alert(url); doAjax(url);PHP里取content会有一个undefined的警告,然后content的值是空的。 用utf-8编码。什么都可以放! header('Content-Type:text/html;charset=UTF-8');已经用了 set names "utf8";并且表啥的都设置成utf8 一定可以。实在不行就贴出sql出来大家瞧瞧 我贴个测试代码吧!<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>无标题文档</title><script type="text/javascript">var get = { byId: function(id) { return typeof id === "string" ? document.getElementById(id) : id }};var xmlhttp;function createXHR(){ try { xmlhttp=new ActiveXObject("Msxm12.XMLHTTP"); //支持不同浏览器版本,下同 } catch(e) { try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { try { xmlhttp=new XMLHttpRequest(); if(xmlhttp.overrideMimeType) { xmlhttp.overrideMimeType("text/xml"); } } catch(e){} } } if(!xmlhttp) { return false; }}//发送请求function doAjax(url){ //alert("Here we are!"+url); createXHR(); xmlhttp.onreadystatechange=requestAjax; alert("Here we are!"+url); xmlhttp.open( "GET", url , true ); alert("Here we are!"+url); xmlhttp.send( null ); //加延迟 //window.setTimeout(function(){xmlhttp.send( null );},1); //alert("Here we are!");}//取得请求function requestAjax(){ if(xmlhttp.readyState==4) { if(xmlhttp.status==200) { //显示发布成功 document.getElementById('statue').innerHTML = xmlhttp.responseText; } }}function send(){ var oConBox = get.byId("conBox"); var content = oConBox.value; var url = 'test1.php?url='+content; alert(url); doAjax(url);}</script></head><body> <input id="conBox" name= "conBox" /> <input id="sendBtn" type="button" onclick="send()" /> <div id="statue"> </div> </body></html><?php header('Content-Type:text/html;charset=UTF-8');//避免输出中文乱码,linux下不需要 $url = $_GET['url']; echo $url;?>这个是自己写的测试代码,输入框输入正常的内容下面会AJAX显示输入的内容。而AJAX要用到URL传值,PHP就GET不到URL里出现的#、&和+尤其#出现的话后面再跟别的也不能显示了,+后面的还能显示。这个问题怎么解决啊。我要GET到这些特殊符号。 将url进行下编码,然后再请求?例如url_encode() PHP就GET不到URL里出现的#、&和+那肯定就要用编码啦....搞半天是这里. 在js传值时要把'#'替换成'%23'~if ( content == '#' ) content = '%23';var url = 'insert.php?author='+author+ '&imgurl=' +imgurl+ '&content='+content; 也或者var url = 'insert.php?author='+author+ '&imgurl=' +imgurl+ '&content='+content.replace("#", "%23"); 楼主昨日得知托福分数太过悲痛。一狠心直接睡了。早上起来研究了下JQUERY。然后用了JQUERY就不存在特殊字符的问题了。仔细想了下应该是JQUERY封装的机制考虑到了这些就做了处理。自己原来的AJAX代码并没有对这方面有很好的处理。不过我也试了下,原来直接写的AJAX代码只要输入的东西拿过来编一下码就可以了。var content = encodeURIComponent(oConBox.value);总结一下JQuery确实好用。像楼主这样花了一天时间纠结在这个问题上。很多人上手就用JQUERY可能根本不会碰到这种问题。谢谢大家啊!!! 我想实现异步刷新数据的功能 本人菜鸟一个问下php安全模式下的问题???急啊? !!!很棘手的分页问题!!! php提交表单后出现http500错误 高分求教PHP中文件登录到mysql时出现的问题,急用。。。在线等。。。。 求一个替换字符串的正则表达式 分页函数:链接部分没有显示出来! 结果只是"显示第 <1>-<5> 条记录,共<9>条记录"这一部分 表单网页过期,怎么解决 框架传递值问题 询问两个问题:200分!不够再加!! conn文件错误问题 求助 没有值问题 DB值问题
是mysql...
我刚才直接去写sql语句发现可以插#但是插'的确不行,单数的'都会有错,双数的'才行,要加/才行。
另外纠正下我插#的时候不是插不进去,是插进去一个空的。
现在不能插入数据库的是#和&,数据库里取到的是空值。
肯定是因为URL的问题。大家有谁知道什么原因么?
//alert(content);
var url = 'insert.php?author='+author+ '&imgurl=' +imgurl+ '&content='+content;
//alert(url);
doAjax(url);
PHP里取content会有一个undefined的警告,然后content的值是空的。
header('Content-Type:text/html;charset=UTF-8');
已经用了
set names "utf8";
并且表啥的都设置成utf8
一定可以。实在不行就贴出sql出来大家瞧瞧
我贴个测试代码吧!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>无标题文档</title>
<script type="text/javascript">
var get = {
byId: function(id) {
return typeof id === "string" ? document.getElementById(id) : id
}
};var xmlhttp;
function createXHR()
{
try
{
xmlhttp=new ActiveXObject("Msxm12.XMLHTTP"); //支持不同浏览器版本,下同
}
catch(e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
try
{
xmlhttp=new XMLHttpRequest();
if(xmlhttp.overrideMimeType)
{
xmlhttp.overrideMimeType("text/xml");
}
}
catch(e){}
}
}
if(!xmlhttp)
{
return false;
}
}//发送请求
function doAjax(url)
{
//alert("Here we are!"+url);
createXHR();
xmlhttp.onreadystatechange=requestAjax;
alert("Here we are!"+url);
xmlhttp.open( "GET", url , true );
alert("Here we are!"+url);
xmlhttp.send( null );
//加延迟
//window.setTimeout(function(){xmlhttp.send( null );},1);
//alert("Here we are!");
}//取得请求
function requestAjax()
{
if(xmlhttp.readyState==4)
{
if(xmlhttp.status==200)
{
//显示发布成功
document.getElementById('statue').innerHTML = xmlhttp.responseText;
}
}
}function send(){
var oConBox = get.byId("conBox");
var content = oConBox.value;
var url = 'test1.php?url='+content;
alert(url);
doAjax(url);
}
</script>
</head><body>
<input id="conBox" name= "conBox" /> <input id="sendBtn" type="button" onclick="send()" />
<div id="statue"> </div>
</body>
</html>
<?php
header('Content-Type:text/html;charset=UTF-8');//避免输出中文乱码,linux下不需要
$url = $_GET['url'];
echo $url;
?>
这个是自己写的测试代码,输入框输入正常的内容下面会AJAX显示输入的内容。
而AJAX要用到URL传值,PHP就GET不到URL里出现的#、&和+
尤其#出现的话后面再跟别的也不能显示了,+后面的还能显示。这个问题怎么解决啊。我要GET到这些特殊符号。
在js传值时要把'#'替换成'%23'~
if ( content == '#' ) content = '%23';
var url = 'insert.php?author='+author+ '&imgurl=' +imgurl+ '&content='+content;
var url = 'insert.php?author='+author+ '&imgurl=' +imgurl+ '&content='+content.replace("#", "%23");
总结一下JQuery确实好用。像楼主这样花了一天时间纠结在这个问题上。很多人上手就用JQUERY可能根本不会碰到这种问题。
谢谢大家啊!!!