I am making a form for (Chinese) users to fill in the name. Its a php app, using mysqli, my code is like this:
<?php
$link = mysqli_connect("127.0.0.1", "test", "test", "db");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
if (!$link->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $linki->error);
} else {
printf("Current character set: %s\n", $link->character_set_name());
}
// someone told me to do this.
if ($_SERVER['REQUEST_METHOD']=="POST")
{
echo "POST";
$displayname=$_POST["displayname"];
$stmt = $link->prepare("INSERT INTO user(name) VALUES (?)");
//var_dump($stmt);
$stmt->bind_param('s',$displayname);
$stmt->execute();
#echo "Success create user";
printf("Error: %s.\n", $stmt->error);
# Here's the output, when I filled the form;
# Current character set: utf8 POSTError: Incorrect string value: '\xE4\xBD\xA0\xE5\xA5\xBD' for column `db`.`user`.`name` at row 1.
# curl cant work either.
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<body>
<form action="<?php echo htmlspecialchars($_SERVER['SCRIPT_NAME']); ?>" method="post">
<input name="displayname" type="text" id="displayname">
<label for="displayname">chinese</label>
</div>
<button type="submit">submit</button>
</form>
</body>
</html>
And i got sth like this
Current character set: utf8
POSTstring(6) "首先"
Error: Incorrect string value: '\xE9\xA6\x96\xE5\x85\x88' for column db
.user
.name
at row 1.
What I am thinking is that since the var_dump result is correct, so its sth to do with prepared statement.
UPDATE: when i tried to manually urlencode the data to Displayname=%E5%BC%A0%E4%B8%89 and curl post it, the results were correct.