我需要在url中直接带sql语句,插入到数据库,
http://localhost/insert.php?s=insert+into+members+(uid,username)+values+(null,'蓝色')这样插入的是乱码,中文变成了   %C0%B6%C9%AB   ,英文和数字一切正常。请问如何解决,谢谢<?php
include("conn.php");
       $s=urlencode($_GET['s']);
       $s = str_replace('+','%20',$s);
       $s = str_replace('%2C',',',$s);
       $s = str_replace('%27','\'',$s);
       $s = str_replace('%28','(',$s);
       $s = str_replace('%29',')',$s);
       $s = str_replace('%20',' ',$s);
       $s = str_replace('%5C','',$s);
    $SQL="$s";
  $query=mysql_query($SQL);
  $err=mysql_error(); 
if($err){   
    echo "error:".mysql_error();   
}else{
    echo "success"; 
}
mysql_close($conn); 
?>

解决方案 »

  1.   

    $s=urlencode($_GET['s']); ?
    为什么要对传递过来的值进行 urlencode?就算是要也是 urldecode吧?
      

  2.   

    你这个是 JS 传递的 数据吧?
    http://localhost/insert.php?s=insert+into+members+(uid,username)+values+(null,'蓝色')js 传递过来 看你的页面 数据库是不是UTF-8,是的话可以直接插数据库的.
      

  3.   

    数据库是gbk的,这个是直接执行的HTTP页面,没有从别的地方提交
      

  4.   

    diao.带sql数据的参数,怕别人看不到你表结构么?
      

  5.   

    那你的sql 不能直接写吗?http://localhost/insert.php?s=insert into members (uid,username) values (null,'蓝色')header ( "Content-type: text/html; charset=gbk" );
    $s=$_GET['s'];
    echo $s;//debug
    $SQL=$s;
    mysql_query( "set   names   'GBK ' ");
    mysql_query($SQL);//=============
    这样就可以了吧?
      

  6.   

    发送前用encodeURIComponent对参数编码另外你的SQL语句可以在服务器端构建的
      

  7.   

    输出来的是  insert into pre_ucenter_members (uid,username) values (null,\'蓝色\')
    插入到数据库还是乱码
      

  8.   

    呵呵,就算联系也不带直接在地址栏里面写sql语句的吧
      

  9.   


    你数据库\数据表 应该不是gbk编码吧?确认弄清楚
      

  10.   

    全部使用统一的编码
    UTF-8
      

  11.   

    谢谢各位,我已经解决了,原来是 ' 转义的问题<?php
    include("conn.php");
    $s=urldecode($_GET['s']);
    $s=str_replace('\"','\'',$s);
    $SQL="$s";
      $query=mysql_query($SQL);
    ...
    mysql_close($conn); 
    ?>
      

  12.   

    http://localhost/insert.php?s=insert+into+members+(uid,username)+values+(null,"蓝色")