PHP,MYSQL INSERT INTO umlauts,如ä,ü,ö

时间:2016-04-28 13:22:56

标签: php mysql

我有问题。我几个小时以来一直坐在那个问题上,我找不到任何解决办法。

我将数据库字符集和整理更改为UTF8 - 无法正常工作。

当我插入正常字母时,如果输入中有一个变音符号,则数据库中的输入为空。

代码:

if(isset($_POST['submitSecurityQuestions'])){

$securityQuestion1 = $_POST['securityQuestion1'];
$securityQuestion2 = $_POST['securityQuestion2'];
$securityAnswer1 = $_POST['securityAnswer1']);
$securityAnswer2 = $_POST['securityAnswer2'];

if(empty($securityAnswer1) || empty($securityAnswer2))
{
    $msg = "Du hast nicht alles ausgefüllt!";
} else {
    if($securityAnswer1 < '4' || $securityAnswer2 < '4') {
        $msg = "Du musst mindestens 4 Zeichen eingeben!";
    } else {
        mysql_query("UPDATE users SET securityQuestions = '1' WHERE id = '".$user->id."' LIMIT 1") or die(mysql_error());                   
    mysql_query("INSERT INTO `users_securityquestions` (`securityQuestion1`, `securityQuestion2`, `securityAnswer1`, `securityAnswer2`, `createdTime`, `createdIP`, `createdUserAgent`, `active`, `userID`) VALUES ('".$securityQuestion1."', '".$securityQuestion2."', '".$securityAnswer1."', '".$securityAnswer2."', '".$time."', '".$ip."', '".$user_agent."', '1', '".$user->id."');") or die(mysql_error());
        header('location:'.$_SERVER['PHP_SELF']);
    }
} }

我也试过过滤器:

function FilterText($str) {
    if(get_magic_quotes_gpc()){ $str = stripslashes($str); }
    $str = preg_replace(array('/\x{0001}/u','/\x{0002}/u','/\x{0003}/u','/\x{0005}/u','/\x{0009}/u'),' ',$str);
    $str = mysql_real_escape_string($str);
    return $str;
}

如何将ä或ü等元音变元插入数据库? 我正在使用MySQL。

谢谢!

4 个答案:

答案 0 :(得分:0)

尝试在插入之前运行此查询:

SET NAMES utf8;

这将告诉MySQL服务器传入的数据也是utf8编码的。

答案 1 :(得分:0)

在操纵查询之前先输入'SET NAMES utf8'。另外,请确保您的文件编码为utf8

if(isset($_POST['submitSecurityQuestions'])){

$securityQuestion1 = $_POST['securityQuestion1'];
$securityQuestion2 = $_POST['securityQuestion2'];
$securityAnswer1 = $_POST['securityAnswer1']);
$securityAnswer2 = $_POST['securityAnswer2'];

if(empty($securityAnswer1) || empty($securityAnswer2))
{
    $msg = "Du hast nicht alles ausgef&uuml;llt!";
} else {
    if($securityAnswer1 < '4' || $securityAnswer2 < '4') {
        $msg = "Du musst mindestens 4 Zeichen eingeben!";
    } else {
        mysql_query('SET NAMES utf8');
        mysql_query("UPDATE users SET securityQuestions = '1' WHERE id = '".$user->id."' LIMIT 1") or die(mysql_error());                   
    mysql_query("INSERT INTO `users_securityquestions` (`securityQuestion1`, `securityQuestion2`, `securityAnswer1`, `securityAnswer2`, `createdTime`, `createdIP`, `createdUserAgent`, `active`, `userID`) VALUES ('".$securityQuestion1."', '".$securityQuestion2."', '".$securityAnswer1."', '".$securityAnswer2."', '".$time."', '".$ip."', '".$user_agent."', '1', '".$user->id."');") or die(mysql_error());
        header('location:'.$_SERVER['PHP_SELF']);
    }
} }

答案 2 :(得分:0)

尝试插入&amp;更新

$securityQuestion1 = utf8_encode($_POST['securityQuestion1']);
$securityQuestion2 = utf8_encode($_POST['securityQuestion2']);
$securityAnswer1 = utf8_encode($_POST['securityAnswer1']);
$securityAnswer2 = utf8_encode($_POST['securityAnswer2']);

从数据库中提取数据时

对每个数据使用utf8_decode()函数..

答案 3 :(得分:0)

好吧,我刚刚遇到了类似的问题,刚刚通过添加解决了

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">

在标题中。

我的数据库排序规则是 utf8_general_ci