6

I had a registration form which will insert into a member table, the collation is in utf8_general_ci, and I use SET NAMES utf8 in php script, OK here is the problem, I can't insert a string other than pure alphabet, I try input 'Hélène' in a form field, after the sql query run, I check with my db table, the field is inserted as 'H', the rest of the alphabet cannot be insert whenever the string come with special alphabet such as é, ř on behind.

Can someone please help? thanks.

SOLUTION:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

mysql_query("SET NAMES utf8");

above two line is crucial part to accept input into database and output on a webpage.

Thanks everyone for the advise.

conmen
  • 2,377
  • 18
  • 68
  • 98
  • 3
    Read about encodings and their handling in these two great texts and you will surely find the problem: http://kunststube.net/frontback/ and http://kunststube.net/encoding/. – Marko D Feb 20 '13 at 09:57
  • What is the encoding of the DB table? Make sure it's also utf-8. Also check that your script receives correct string. Echo the form data before inserting it to the DB. (After validation.) – ZZ-bb Feb 20 '13 at 10:01
  • Look solution here [SET NAMES utf8 in MySQL?][1] [1]: http://stackoverflow.com/questions/2159434/set-names-utf8-in-mysql – sanj Feb 20 '13 at 10:04
  • @Marko D, thanks, these links are help! – conmen Feb 22 '13 at 03:07
  • Solution doesn't go in question. It goes as answer. – Marcus Adams Feb 11 '15 at 21:08
  • possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – Marcus Adams Feb 11 '15 at 21:10
  • Great article, Marko D. By the time I read it though, the mysql commands were outdated and should be replaced with their mysqli counterparts. Specifically, mysqli_set_charset($dbc, 'utf8'); (note that the order of the parameters has been inverted since mysql_set_charset('utf8', $dbc);) – Fnord23 Nov 16 '15 at 09:20

3 Answers3

2

try to insert data after encoding. try mysql_real_escape_string() to encode. then execute insert query.

EDIT:-

This answer was posted one year ago. Now mysql_real_escape_string() for php 5 will work in mysqli::real_escape_string this format. please check here

Ripa Saha
  • 2,532
  • 6
  • 27
  • 51
1

And if you don't want to worry about so many different charset codings or if htmlentities doesn't work for you, here the alternative: I used mysqli DB connection (and PHPV5) Form post for writing/inserting to MySQl DB.

$Notes = $_POST['Notes']; //can be text input or textarea.

$charset = mysqli_character_set_name($link);  //only works for mysqli DB connection
printf ("To check your character set but not necessary %s\n",$charset);  

$Notes = str_replace('"', '&quot;', $Notes);  //double quotes for mailto: emails.  
$von = array("ä","ö","ü","ß","Ä","Ö","Ü"," ","é");  //to correct double whitepaces as well
$zu  = array("&auml;","&ouml;","&uuml;","&szlig;","&Auml;","&Ouml;","&Uuml;","&nbsp;","&#233;");  
$Notes = str_replace($von, $zu, $Notes);  
echo " Notes:".$Notes."<br>" ;  
$Notes = mysqli_real_escape_string($link, $Notes); //for recommended mysqli DB connection.
//$Notes = mysql_real_escape_string($Notes); //for old deprecated mysql DB connection.

// mysqli_real_escape_string Escapes special characters in a string for use in an SQL statement

echo " Notes:".$Notes ;  //ready for inserting
KarlosFontana
  • 188
  • 2
  • 7
0

The reason you are not able to do so is because PHP prevents a set of special characters to be sent to database to avoid things like sql injection. You can use the escape string function if you wish to escape but the best way to do so is by using the prepared statements.

$connection = <"Your database connection">;
$userinput = "Special characters Hélène";
$stmt = $con->prepare("insert into `table` (`fieldname`)values(?)");
$stmt->bind_param("s",$input);
$stmt->execute();
Vaibhav Tomar
  • 107
  • 1
  • 5