php - HTML 表单中的西类牙字符在 MySQL 数据库中显示错误

标签 php mysql forms character-encoding

因此,我通过 HTML 表单将数据发送到我的 MySQL 数据库。

当我发送包含“ñ”或重音符号“á、é、í..”等特殊字符的单词时,当我检查表格中的结果时,这些字符显示为“ã±”。

我几乎尝试了一切:

我的 html 表单页面标题有

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

表单接受 UTF-8 格式 <form accept-charset="UTF-8">

我还添加了我的操作 php 脚本:

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

然后我尝试了htmlentities

但是所有的“ñ”都显示为&atilde;&

我的数据库、表和字段设置为 utf8_general_ci,也尝试过 utf8_spanish_ci 但没有任何改变。

我不知道还能做什么,我还缺少什么吗?

这是我正在使用的 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);

?>

最佳答案

您的错误是使用非多字节安全字符串函数:

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

使用multi-byte string functions相反。

除此之外,您的表单很容易受到 SQL injection 的攻击,您急需的东西fix .

关于php - HTML 表单中的西类牙字符在 MySQL 数据库中显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3952700/

相关文章:

php - mysql 查询没有给我正确的输出

php - 如何从html输入向MySQL插入日期?

php - 使用 LIKE 在 SELECT mysql 语句中查找数字代码字符串

python - Django Forms cleaned_data 缺少某些字段

php - 发布 Laravel 表单不起作用

C# 如何在屏幕上的特定鼠标位置显示窗体?

php - 使用 php 进行动态导航

PHP 哈希文件,CRC32b 或 MD5 更好?

MySQL - 关于某些表的正确方法和性能

mysql - 从数据库中检索(选择)数据的有效方法是什么?