0

I have a problem. Im sitting on that problems since hours and I cant find any solution.

I changed Database Character Set and Collation to UTF8 - not working.

When I insert normal letters its working but if one umlaut is in the input, the input is empty in database.

Code:

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']);
    }
} }

I also tryed that Filter:

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;
}

How can I insert Umlauts like ä or ü into database? I'm using MySQL.

Thanks!

  • 9
    Have a look through this http://stackoverflow.com/questions/279170/utf-8-all-the-way-through – Funk Forty Niner Apr 28 '16 at 13:23
  • 2
    You have to switch everything to UTF8 for this to work seamessly. Check database connection collation and your pages encoding. You must serve UTF8 pages and receive UTF8 forms. – Ghigo Apr 28 '16 at 13:25
  • 2
    1) forget the mysql extension (deprecated). 2) read about SQL injection, 3) read about mysqli or PDO and prepared statements. Convert EVERYTHING to utf8 (includiong your files, the db connection, and everything else). – Pred Apr 28 '16 at 13:25
  • change coloum collation to `utf8_general_ci` – Vivek Singh Apr 28 '16 at 13:28
  • @Vicky in this case it would be better to use [utf8mb4](https://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html) – Machavity Apr 28 '16 at 13:37

4 Answers4

0

Try running this query before your inserts:

SET NAMES utf8;

This will tell the MySQL server that the incoming data is also utf8 encoded.

apriede
  • 106
  • 7
0

Put 'SET NAMES utf8' Before your manipulating queries. Also, be sure your file encoding is 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']);
    }
} }
Mojtaba
  • 4,852
  • 5
  • 21
  • 38
0

Try for insert & update

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

When fetching data from database

Use utf8_decode() function for each data..

Mohammedshafeek C S
  • 1,916
  • 2
  • 16
  • 26
0

Well I just had a similar problem and it has just been solved by adding

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

in the header.

My database collation is utf8_general_ci

hans-könig
  • 553
  • 8
  • 10