php - 通过 html 表单和 php 将外国 lang 数据插入 mysql db 表

标签 php html mysql forms

<分区>

我把外来语言通过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");

最佳答案

在 PHP 页面的开头声明 UTF-8 作为默认字符集:

<?php
  header('Content-Type: application/json;charset=utf-8');
?>

也通过http://php.net/manual/en/book.mbstring.php彻底阅读多字节字符串(这意味着您要查找的内容)。

关于php - 通过 html 表单和 php 将外国 lang 数据插入 mysql db 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33722399/

相关文章:

php - 在 PHP 中将 mysql 中的数字转换为文本

php - Joomla 使用 JDatabase 插入数据,其中表名存储在 php 变量中

php - php判断字符串是否至少有一个字母、数字和特殊字符

php - 如何在mysql中的关系数据中的每个父记录上选择n个记录子记录

php - 使用 HTML PHP MySQL 的下拉导航菜单

php - 如何设置扩展 WordPress 编码标准的 PHP CodeSniffer + VSCode 中的自动修复错误?

jquery - 使用 jQuery 滚动 div "left and right"和 "up and down"

java - 站点资源泄漏到 GetMapping 中的 UUID

html - 将我的页面分成 2 个垂直列(html 或 css)

MySQL从三个表中过滤行