2014-05-11 47 views
0

我想將一個UTF-8字符串發送到Mysql數據庫,但Mysql不支持UTF8。使用MYSQL/PHP的UTF-8問題

是我的錯?

這是我的代碼:

這是HTML/PHP頁面:

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

<form action="" method="post" enctype="multipart/form-data" accept-charset="UTF-8"> 
    <input type="text" name="name" /> 
    <input type="submit" value="submit"> 
</form> 
</body> 

,這是第一個PHP函數來創建一個SQL表:

function createFolowerTable() 
{ 
    $con=mysqli_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD,DATABASE_NAME); 
    mysqli_set_charset('utf8',$con); 

    //Type: img/file/doc/audio/video 
    $sql="CREATE TABLE IF NOT EXISTS `follow` (
     `Followid` int(10) NOT NULL AUTO_INCREMENT, 
     `fname` varchar(20) NOT NULL, 
     `lname` varchar(20) NOT NULL, 
      PRIMARY KEY (`Followid`)) 
      ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=22 ;"; 

    if (!mysqli_query($con,$sql)) { 
     echo "Error creating table: " . mysqli_error($con); 
    } 
    mysqli_close($con); 
} 

而這個函數向mysql添加數據:

$con=mysqli_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD,DATABASE_NAME); 

$query_insert_user = "INSERT INTO `members` (`fname`, `lname`) 
            VALUES ('$fname', '$lname')"; 


    $result_insert_user = mysqli_query($con, $query_insert_user); 
    if (!$result_insert_user) { 
     $_state=false; 
    } 
    mysqli_close($con); 
+0

您連接每次,你必須執行'mysqli_set_charset('utf8',$ con);',你在插入數據時忘了這麼做。 –

回答

2

您應該運行後,下面的查詢的mysql_connect

mysqli_query($con, "SET NAMES 'utf8'"); 

您還應該使用在$ FNAME至少mysqli_real_escape_string和$ L-NAME如果要是沒有在你的代碼中完成,它已經