0

I wrote a PHP file which should insert values in a MySQL database.

I explain my problem with an example.

include 'db_connect.php';
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET 'utf8'");
$title = 'knödel';
echo $title;
$sql="INSERT INTO RECIPES (title) VALUES ('$title')"; 

In the database, there is 'kn';

I cut the string from the umlaut.

Will Vousden
  • 32,488
  • 9
  • 84
  • 95
user959456
  • 565
  • 1
  • 9
  • 13

1 Answers1

1

If your database collation is Unicode or UTF-8, just do:

$sql="INSERT INTO RECIPES (title) VALUES (N'$title')"; 

The only difference is the N character which makes it possible to insert Unicode data into the database.

And you have to take the security risks seriously. Use mysql_real_escape_string together with other required actions to protect against sql injections. (If you don't do it now, you'll forget it when writing a big real-world application.)

Community
  • 1
  • 1
Hossein
  • 4,097
  • 2
  • 24
  • 46