douzongmu2543 2016-10-14 09:16
浏览 137

PHP Mysql特殊字符问题。 记录没有插入mysql表中

My website http://www.locuciones-baratas.com/ There are many textarea where user can input text. But when user input text with special character like

1.000 millones de pesetas" |#~@¬#~€¬€~¬'''¡¡!"!! "Yo he ahorrado al PP más de 1.000 millones de pesetas", ha asegurado Correa, explicando que esa cifra se corresponde con el

then record won't insert into mysql database

I tried ut8 general ci as character set.

Set meta utf8 in html

$textarea = mb_convert_encoding($_POST['textarea'], 'ISO-8859-1','utf-8');
$insert_array = array('textarea'=>$textarea);
$sql = build_sql_insert('order',$insert_array); 
mysqli_query($conn,$sql) ;

function build_sql_insert($table, $data) {

$key = array_keys($data);
$val = array_values($data);
$sql = "INSERT INTO `$table` (" . implode(', ', $key) . ") " . "VALUES ('" . implode("', '", $val) . "')";
return($sql); 
    }

I think problem may be php dont allow special character to further proceed, data may be don't reach to mysql.

Please advice how to insert record with such text

  • 写回答

2条回答 默认 最新

  • douwei9759 2016-10-14 10:30
    关注

    maybe you need to escape strings.

    Try adding mysqli_real_escape_string.

    function build_sql_insert($table, $data, $conn) {
    
        $key = array_keys($data);
        $val = array_values($data);
    
        //Mysql real escape string use a string value. this hack will work only 
        //for a single value passed. please read the comment below.
        $val[0] = mysqli_real_escape_string($conn, $val[0]);
    
        $sql = "INSERT INTO `$table` (" . implode(', ', $key) . ") " . "VALUES ('" .     implode("', '", $val) . "')";
        return($sql); 
    }
    

    More information in php manual here

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料