将数据保存到mysql中的奇怪字符数据库

问题描述:

当我从表单中保存数据时,数据被剥离并且奇怪的字符附加了数据。 我无法从代码中找出它。它工作正常,突然发生这件事。当我在它保存为下面的图片数据库中的表中输入[email protected]将数据保存到mysql中的奇怪字符数据库

代码是用PHP编写的

enter image description here

+0

你能否提供PHP代码? – northpole 2012-02-22 21:16:38

在将变量传递给mysql之前,您应该在php trim(http://php.net/manual/en/function.trim.php)中使用。你也必须确保你的mysql表是utf8_general_ci。

例子:

<?php 
$con = mysql_connect("localhost","user","pass"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

mysql_select_db("my_db", $con); 

$mail = trim($_POST['email']); 

mysql_query("INSERT INTO Persons (email) VALUES (".$mail.")"); 

mysql_close($con); 
?> 

mysql_real_escape_string也请为确保您在数据库中插入变量。

这看起来像一个Unicode替换字符如看到Removing unwanted character from column进一步信息

我猜你没有使用函数utf8_encode($输入)。尝试,看看是否你的错误得到纠正。