HTML posted form data gets written as jibberish into MySQL databas HTML发送的表单数据被写入MySQL数据库中的无意义字符。

2024-09-24 00:31:37 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图用希腊字母在文本字段中输入数据时,我的wsgi脚本将数据作为jibberish保存在MySQL数据库中,我不知道为什么。 以下是要通过表单方法过账时的相关代码:

pdata = pdata + '''
<form methods="POST" enctype="multipart/form-data" action="%s">
    <tr>
            <td> <center>   <input type="text"  name="task"     size=50>    </td>
            <td> <center>   <input type="text"  name="price"    size=5>     </td>
            <td> <center>   <input type="text"  name="lastvisit">           </td>
        </table><br><br>
        <td>    <input type="image" src="/static/img/submit.gif" name="update" value="Ενημέρωση!">  </td>
    </tr>
</form>
''' % app.get_url( '/update/<name>', name=name )


pdata = pdata + "<meta http-equiv='REFRESH' content='200;%s'>" % app.get_url( '/' )
return pdata

这里是一个相对回调函数,它试图将发布的表单数据输入到MySQL数据库中。在

^{pr2}$

我不明白为什么数据以jibberish而不是正确的utf-8存储到数据库中。同样尝试使用utf-8编码类型也没有成功。在

<form methods="POST" enctype="utf-8" action="%s">

Tags: 数据textnameform数据库表单inputtype
2条回答

根据wsgi_mod文档,WSGIDaemonProcess的默认编码是ASCII。ASCII中不包括希腊字符,并且您的输入未正确解码。如果要允许使用希腊字符,则必须使用UTF-8或iso-8859-1。通常服务器是由init系统启动的deamons,99%的情况下仍然使用ASCII作为默认编码。在开发或调试时,通常不会遇到这些问题,因为python脚本继承了当前用户的环境,而当前用户通常使用UTF-8。在

$env
.....
LANG=en_GB.UTF-8
.....

引用apache的wsgi_mod:

lang=locale Set the current language locale. This is the same as having set the LANG environment variable. You will need to set this on many Linux systems where Apache when started up from system init scripts uses the default C locale, meaning that the default system encoding is ASCII. Unless you need a special language locale, set this to en_US.UTF-8. Whether the lang or locale option works best can depend on the system being used. Set both if you aren’t sure which is appropriate.

locale=locale Set the current language locale. This is the same as having set the LC_ALL environment variable. You will need to set this on many Linux systems where Apache when started up from system init scripts uses the default C locale, meaning that the default system encoding is ASCII. Unless you need a special language locale, set this to en_US.UTF-8. Whether the lang or locale option works best can depend on the system being used. Set both if you aren’t sure which is appropriate.

The html form data to be posted is "αυτή είναι μια δοκιμή" and the end result inside database is "αÏÏή είναι μια δοκιμή"

然而,“αΓτήείναιμιαδοκιμή”显然是无效的UTF-8,因为 第38位的字节(ή)表示它是一个两字节的UTF-8字符,但后面只有1个字节(reference)。在

如果这是传递给代码的数据,那么您需要检查并确认HTML表单以正确的UTF-8格式提交数据。在

<form accept-charset='UTF-8'>

假设您的输入字符串是正确的UTF-8编码,那么您的输出字符串“ÎÏÎÎÎÎνÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎ。在

因此,可以将问题(或传输形式)定义为上述的存储格式或HTML格式。在

Yes MySQL Tables and Columns are configured to be utf8_general_ci.

这也可能是个问题。MySQLutf8_而不是完整的UTF-8(wat?!),因为它是3字节而不是4字节;因此,如果您存储了一个4字节的UTF-8字符,它将偏移所有后面的字符字节,使它们看起来像垃圾。在

解决方案:

将MySQL列和所有排序规则更新为utf8mb4_unicode_ci

还要检查以确保MySQL传输机制也在使用utf8mb4_。在

还有请Read This

相关问题 更多 >