我通过html表单将外来的lang插入到mysql db中,结果如下。
యేసౠకà±à°°à±€à°¸à±à°¤à± జననమà±
当我通过以下声明插入数据时,它可以正常工作。
$conn->set_charset('utf8');
$sql = INSERT INTO table_name (column1, column2) VALUES (value1,value2);
我使用html表单插入数据时遇到问题。
这是我使用的代码。
<?php
$db_database = 'xxxxx';
$db_hostname = 'xxxx';
$db_username = 'xxxxx';
$db_password = 'xxxxx';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
mysql_select_db($db_database)
or die("Unable to connect to database: " . mysql_error());
$title =$_POST['title'];
$lang =$_POST['lang'];
mysql_query ("set character_set_results='utf8'");
$sql="INSERT INTO lyrics_a (title, lang)VALUES('$title', '$lang')";
$result=mysql_query($sql);
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
Html表格:
<form method="post" action="/insertdata.php">
<label>TITLE</label>
<input name="title" type="text" id="title" class="form-control">
<label>LANGUAGE</label>
<input name="lang" type="text" id="lang" class="form-control">
<button type="submit" name="submit" id="submit" value="Update">Submit</button>
</form>
为我工作。希望这有助于他人。
我对代码进行了更改,如下所示:
Include below lines in mysql statements.
mysql_connect();
mysql_query("SET NAMES 'utf8'");
Change <form> as below.
<form method="post" action="/insertmysql.php" enctype="multipart/form-data" accept-charset="UTF-8">
Add the below line at the top of the forms PHP page.
header("Content-Type: text/html;charset=UTF-8");
答案 0 :(得分:0)
在PHP页面的开头将UTF-8声明为默认字符集:
<?php
header('Content-Type: application/json;charset=utf-8');
?>
还要仔细阅读http://php.net/manual/en/book.mbstring.php以了解多字节字符串(这意味着你要找的东西)。