简体   繁体   English

具有西班牙语字符的HTML表单在MySQL数据库中显示错误

[英]HTML form with spanish characters displayed wrong in MySQL database

So I'm sending data to my MySQL database through a HTML form. 因此,我正在通过HTML表单将数据发送到我的MySQL数据库。

When I send a word with special characters like "ñ" or accents "á, é, í.." and when I check the result in the tables those characters are displayed as "ã±". 当我发送带有特殊字符(如“ñ”或带有重音符号“á,é,í..”)的单词时,当我在表格中检查结果时,这些字符将显示为“ã±”。

I tried pretty much everything: 我几乎尝试了所有事情:

my html form page header has 我的html表单页面标题有

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

The form accepts UTF-8 with <form accept-charset="UTF-8"> 表单使用<form accept-charset="UTF-8">

I also added to my action php script: 我还添加了我的动作PHP脚本:

mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET'utf8'");

Then I tried htmlentities 然后我尝试了htmlentities

but all the "ñ" were displayed as &atilde;& 但所有的“ñ”都显示为&atilde;&

My database, tables, and fields are set in utf8_general_ci, also tried utf8_spanish_ci but nothing changed. 我的数据库,表和字段都在utf8_general_ci中设置,也尝试过utf8_spanish_ci,但没有任何更改。

I don't know what else to do, is there anything else I am missing here? 我不知道该怎么办,这里还有什么我想念的吗?

This is the PHP script I'am using: 这是我正在使用的PHP脚本:

<?php
// Make a MySQL Connection
$con = mysql_connect("I DELETED THIS");
if (!$con)
{
die('No se pudo conectar a la BD: ' . mysql_error() );
}

mysql_select_db("ldma", $con);

$nombre  = ''; 
$ape1   = ''; 
$ape2   = ''; 
$apodo = ''; 
$errmsg = '';

if ($_POST['enviado']==1)
{
   $nombre   = $_POST['nombre'];
   $ape1   = $_POST['ape1'];
   $ape2 = $_POST['ape2'];
   $apodo = $_POST['apodo'];

   $permitidos = "abcdefghijklmnopqrstuvwxyzÁÉÍÓÚÜüáéíóúñÑABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
   for ($i=0; $i<strlen($nombre); $i++)
   { 
      if (strpos($permitidos, substr($nombre,$i,1))===false)
      {$errmsg = 1; }
         else
         {$errmsg = '';} 


   } 

    /*if (ereg('^[ a-zA-Zñ]+$', $nombre)) 
        {$errmsg = '';
        } 
        else 
        {$errmsg = 1;
        }*/


    if(strlen($nombre) == 0 || strlen($nombre) <= 3) 
    {$errmsg = 1;} 
        else if (strlen($nombre) > 20) 
            {$errmsg = 1;} 

    if(strlen($ape1) == 0 || strlen($ape1) <= 3) 
    {$errmsg = 1;} 
        else if (strlen($ape1) > 15) 
            {$errmsg = 1;} 

    if(strlen($ape2) == 0 || strlen($ape2) <= 3) 
    {$errmsg = 1;} 
        else if (strlen($ape2) > 15) 
            {$errmsg = 1;} 

    if(strlen($apodo) > 15)
    {$errmsg = 1;} 



    if($errmsg == '')
    {

    // Insert a row of information into the table "example"

    $arr = array("nombre", "ape1", "ape2", "apodo"); 
    foreach ($arr as $field)
    {
        $_POST["$field"] = utf8_encode($_POST["$field"]);
        $_POST["$field"] = strtolower($_POST["$field"]);
        $_POST["$field"] = ucwords($_POST["$field"]);
    }

    $sql= "INSERT INTO **** (nombre, ape1, ape2, apodo) 
            VALUES ('$_POST[nombre]', '$_POST[ape1]', '$_POST[ape2]', '$_POST[apodo]')" ;

    if (!mysql_query($sql, $con))
        {
        echo "<center><br /><h2>Al parecer este maestro ya existe, intenta de nuevo.</h2></center> ";
        }
        else {
            echo "<center><br /><h2>Gracias, el maestro ha sido agregado. </h2></center> ";
            }
    }

}

if($errmsg == 1)
    { echo "<center><br/><br/><h2><u>Error: Revisa los datos e intenta de nuevo.</u></h2> </center>
            <center><h2>Recuerda que es recomendable </h2> </center> 
            <center><h2>activar JavaScript en las opciones de tu navegador. </h2></center>";
    }

/*if ($_POST['enviado'] != 1)
    {
    echo 'Error: No se agregaron los datos';
    }*/


mysql_close($con);

?>

Your error is the usage of non-multibyte-safe string functions: 您的错误是使用非多字节安全字符串函数:

 $_POST["$field"] = strtolower($_POST["$field"]);
 $_POST["$field"] = ucwords($_POST["$field"]);

Use multi-byte string functions instead. 请改用多字节字符串函数

Apart from that, your form is vulnerable to SQL injection , something that you need to urgently fix . 除此之外,您的表单容易受到SQL注入的攻击,这是您急需解决的问题

just use mysql_real_escape_string() no need to do anything else. 只需使用mysql_real_escape_string()即可,而无需执行其他任何操作。 for example: mysql_real_escape_string($user), mysql_real_escape_string($password)); 例如:mysql_real_escape_string($ user),mysql_real_escape_string($ password));

Why are you so worried about how your data is displayed in phpmyAdmin? 您为什么如此担心您的数据如何在phpmyAdmin中显示? The important thing is that you get the right display when you show them again in a html page. 重要的是,当您在html页面中再次显示它们时,您将获得正确的显示。

you may eventually get correct display in database but then you may have wrong display on html 您最终可能会在数据库中获得正确的显示,但随后在html上可能显示错误


But i think set names do the job, it did for my similar problem even with string 但是我认为集合名称可以完成这项工作,即使使用字符串也可以解决我的类似问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM