0

When I want to input some Chinese character into my form,

<html>
<title>
Form
</title>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
</head>
<body>
<div id="wrapper">
<form action="form.php" method="post" accept-charset="UTF-8" >
<p>Input: <input type="text" name="input1" /></p>
<input type="submit" value="Submit" />
</form>


</div>
</body>
</html>`

and the form.php:

<?php
header('Content-Type: text/html; charset=utf-8');

define('DB_NAME','form1');
define('DB_USER','root');
define('DB_PASSWORD','');
define('DB_HOST','localhost');

$link= mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);

if (!$link){
    die('could not connect server'.mysql_error());  
} 

$db_selected = mysql_select_db(DB_NAME,$link);

if(!$db_selected){
    die('Cannot connect'.DB_NAME.': '.mysql_error());
}

$value=$_POST['input1'];

$sql="INSERT INTO form (input1) VALUES ('$value')";

if (!mysql_query($sql)){
    die('Error: '.mysql_error());
}


mysql_close();
header("Location: form.htm");
?>

Mysql sheet output something strange like 早&#2621 Why? Should I do something on the code or on Mysql? Please help!!!! Thanks!!!!!!

  • 2
    Possible duplicate of http://stackoverflow.com/questions/14456313/cant-insert-chinese-character-into-mysql – Shakti Phartiyal Mar 06 '17 at 12:17
  • Possible duplicate of [Can't insert Chinese character into MySQL](http://stackoverflow.com/questions/14456313/cant-insert-chinese-character-into-mysql) – Petr Hejda Mar 06 '17 at 12:21
  • Possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – CBroe Mar 06 '17 at 12:24
  • Do _not_ use the `mysql_*` interface; switch to PDO or `mysqli_*`. – Rick James Mar 06 '17 at 23:23
  • `਽` is a "html entity"; how did the `;` vanish? Let's see the SQL that put the string into the table and got it out. – Rick James Mar 06 '17 at 23:24

2 Answers2

0

Did you try to use this line after your database connection ?

mysql_query("SET NAMES utf8");
Guillaume Sainthillier
  • 1,655
  • 1
  • 9
  • 13
0

You can try this.

mysql_set_charset('utf8', $link);

Like this...

<?php
header('Content-Type: text/html; charset=utf-8');

define('DB_NAME','form1');
define('DB_USER','root');
define('DB_PASSWORD','');
define('DB_HOST','localhost');

$link= mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
mysql_set_charset('utf8', $link);
if (!$link){
    die('could not connect server'.mysql_error());  
} 
...
...
Pravin Vavadiya
  • 3,195
  • 1
  • 17
  • 34