一个简单的数据库插入界面和后台
add.html
<script type="text/javascript" language="javascript">
function onAddItem()
{
var xmlhttp;
var workcontent = $("#tb_content").attr("value");
var contact = $("#tb_contact").attr("value");
var re = $("#tb_re").attr("value");
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
var requestStr = "add.php?content="+workcontent + "&contact="+contact +"&re="+re;
xmlhttp.open("GET",requestStr,true);
xmlhttp.send();
alert(requestStr);
}</script>后台 add.php
<?php$workcontent = $_REQUEST["workcontent"];
$contact = $_REQUEST["contact"];
$re = $_REQUEST['re'];include 'conn.php';$sql = "insert into WorkContentTable(workcontent,contact,re) values('$workcontent','$contact','$re')";
$issuccess = @mysql_query($sql);
if(!issuccess){
echo "添加不成功!";
}else{
$str = "添加了一条数据,业务内容为:" + $workcontent + ",联系人为:" + $contact + ",备注为:" + $re;
echo $str;
}
?>mysql的编码为默认的utf-8在firefox浏览器中操作,mysql为中文正常显示
在IE中操作,mysl中文乱码add.php?workcontent=测试&contact=test&re=ss
firefox和ie均echo出乱码本人php新手   这个中文乱码问题搞的真纠结
网上找的方法都试过了  没用  iconv函数转码之后为空串PHPMySQL

解决方案 »

  1.   

    遇到过类似问题,不懂其中原因,当时我这样$bm = array('UTF-8','GBK'); 
     $charTest;
        foreach($bm as $c){ 
            if( $tag === iconv('UTF-8', $c, iconv($c, 'UTF-8', $tag))){//转换编码后是不是相等 
                $charTest =  $c; 
       break;
            } 
        } 
     if($charTest == 'GBK'){
      $tag = iconv('GBK', 'UTF-8', $tag);
     }思路就是PHP接收到参数后两次iconv比较是否相等来判断是否是utf-8,如果不是,就转换当时我的问题是解决了的 楼主可以试试能否解决你的问题
      

  2.   

    谢谢楼上的童鞋
    按照你的方法修改了,奇怪,还是乱码  不过跟之前有点不同
    之前的乱码插入不了mysql,插入中文为空,
    修改之后,起码可以把乱码插入到mysql中
    ORZ....
    上个图吧
    好像上不了图
    这样的乱码   æµ‹è¯•
      

  3.   

    add.php 行首加一句:header("content-type:text/html;charset=utf-8");
      

  4.   

    header("content-type:text/html;charset=utf-8"); 
    加上mysql_query("set names utf8")这句也试过了
    均不行。。
      

  5.   

    你的add.html 加上 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 这句且两个文件都另存为utf-8编码后再试试。
      

  6.   

    header("content-type:text/html;charset=utf-8"); 
    加上mysql_query("set names utf8")这句也试过了
    均不行。。你插入数据时没有 set names utf8,那么读取时 加了更糟你的代码中不是有 echo $str; 吗?改成 
    echo base64_encode($str);
    贴出结果,供分析
      

  7.   

    header("content-type:text/html;charset=utf-8"); 
    加上mysql_query("set names utf8")这句也试过了
    均不行。。你插入数据时没有 set names utf8,那么读取时 加了更糟你的代码中不是有 echo $str; 吗?改成 
    echo base64_encode($str);
    贴出结果,供分析
    输入url为 http://app211.hallapp.3366.com/add.php?workcontent=测试&contact=测试&re=测试echo $str输出为添加了一条数据,业务内容为:����,联系人为:����,备注为:����
    echo base64_encode($str)输出为 5re75Yqg5LqG5LiA5p2h5pWw5o2u77yM5Lia5Yqh5YaF5a655Li677yasuLK1CzogZTns7vkurrkuLrvvJqy4srULOWkh+azqOS4uu+8mrLiytQ=
      

  8.   

    add.html 中定义了了编码为utf8
    add.php中定义了编码为utf8
    mysql中定义了编码为utf8测试如下
    添加英文  IE浏览器ok,firefox ok
    使用add.html添加中文  IE浏览器显示乱码  firefox正常
    使用add.php?content=测试&contact=测试&re=测试   IE浏览器和firefox均显示乱码
      

  9.   

    谢谢你的回复,但是还是不行
    add.html 中定义了了编码为utf8
    add.php中定义了编码为utf8
    mysql中定义了编码为utf8测试结果如上↑
      

  10.   

    不知道你将 header("content-type:text/html;charset=utf-8");  这句加了没有,贴出修改后代码看看。另外,贴出乱码后的截图出来。
      

  11.   

    你的 “业务内容为:”、“,联系人为:”、“,备注为:”是 utf-8 的
    而 3 个“测试”却是 gbk 的
    放在一起能不乱码吗?
      

  12.   

    嗯,修改之后的代码
    add.html<html>
    <head>
    <LINK href="css/style.css" type=text/css rel=stylesheet>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>OSS ID添加工具</title>
    <script language="javascript" type="text/javascript" src="js/jquery.js" ></script><script type="text/javascript" language="javascript">
    function onAddItem()
    {
    var xmlhttp;
    var workcontent = $("#tb_content").attr("value");
    var contact = $("#tb_contact").attr("value");
    var re = $("#tb_re").attr("value");
    if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }else{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
    }
    var requestStr = "add.php?content="+workcontent+"&contact="+contact+"&re="+re+"&sid="+Math.random();
    xmlhttp.open("GET",requestStr,true);
    xmlhttp.send();
    alert(requestStr);
    }</script></head>
    <style>
    .ul_show{list-style:none; width:200px; text-align:center;margin:0 auto;}
    .ul_show li {list-style:none; margin-top:15px; }
    .ul_show li span{width:70px;font-size:14;text-align:left;float:left; }
    #readme {font-size:14; font-weight:bold; }
    </style>
    <body bgcolor="#FFFFFF" text="#000000" onLoad="load()"><div style="text-align:center; margin-top:15px"><div>
          OSS ID添加工具
    </div>
    </br>
    <HR>
    <div id = "readme">
    分配规则:根据所属道具变化类别分配,从道具变化类别*10000+1开始
    </div>
    <div style="margin-top:30px">    请选择要操作的表: <select name="List_Tables" id="List_Tables"  onChange="choose_page();">
        <option value='1'>业务流水表</option>
    </select></div>
    <div id="table2" style="margin-top:30px;">
    <ul class="ul_show">
        <li> <span>workContent:</span> <input type="text" id= "tb_content" style="width:100px;" /> </li>
    <li> <span>contact:</span> <input type="text" id= "tb_contact" style="width:100px;" /> </li>
    <li> <span>re:</span><input type="text" id= "tb_re" style="width:100px;" /> </li>

    </ul>
    </div>
    <div style = "list-style:none;text-align:center;margin:0 auto;">
    </br>
     <button  style="width:100px;" onclick ="onAddItem()">确定添加 </button>

    </div><p>
    <div id="txtHint"><b>User info will be listed here.</b></div>
    </p>
    </div> 
    </body>
    </html>
    add.php<?php
    header("content-type:text/html;charset=utf-8"); 
    $workcontent = $_GET['content'];
    $contact = $_GET['contact'];
    $re = $_GET['re'];include 'conn.php';
    mysql_query("set names utf8");
    $sql = "insert into WorkContentTable(workcontent,contact,re) values('$workcontent','$contact','$re')";
    $issuccess = @mysql_query($sql);
    if(!issuccess){
    echo "添加不成功!";
    }else{
    $str = "添加了一条数据,业务内容为:".$workcontent.",联系人为:".$contact.",备注为:".$re;
    echo $str;
    }
    ?>
      

  13.   

    版主,我试过GBK转UTF  用icov不行
      

  14.   

    嗯,前台和后台的代码都贴出来了,都是utf8编码
    mysql编码也是utf8
      

  15.   

    你给的测试数据是在浏览器地址栏运行的结果,而你实际是ajax方式运行的
    运行的方式不同得到的结果也不同
      

  16.   

    嗯   用ajax回显的数据,firefox正常,IE是乱码
    用url直接传的中文数据,firefox和ie均为乱码
      

  17.   

    你要在出现乱码的场景中,给我乱码数据的 base64 编码
    用 base64 编码的原因是为了防止在复制、粘贴中破坏了原有信息
    你的是 ajax 应用,所以可将
    echo base64_encode($str);
    改作
    file_put_contents('www.txt', base64_encode($str));
    再将 www.txt 的内容贴上来
      

  18.   

    谢谢各位了,提出了很宝贵的意见
    现在使用iconv函数,又可以了。。
    虽然还有点迷惑,但是现在问题还是解决了
    果断结贴给分