<分区>
我把外来语言通过html形式插入到mysql数据库中,结果如下。
యేసౠకà±à°°à±€à°¸à±à°¤à± జననమà±
当我通过以下语句插入数据时,它工作得很好。
$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>
为我工作。希望这对其他人有帮助。
I made changes to my code as below:
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");