2013-02-20 172 views
6

我有一個註冊表格,它將插入到一個成員表中,排序規則在utf8_general_ci,我在php腳本中使用SET NAMES utf8,這裏是問題,我不能插入除純字母表,我嘗試在表單字段中輸入'Hélène',在sql查詢運行後,我使用db表檢查字段被插入爲'H',字符串的其餘部分無法插入,只要字符串帶有特殊字母如é, ř就在後面。MySQL插入特殊字符

有人可以幫忙嗎?謝謝。

SOLUTION:

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

mysql_query("SET NAMES utf8"); 

上述兩個線是關鍵的部分接受在網頁上輸入到數據庫和輸出。

謝謝大家的建議。

+3

閱讀有關編碼以及它們在這兩個偉大的文本處理,你一定會發現問題:http://kunststube.net/frontback/和http://kunststube.net/encoding/。 – 2013-02-20 09:57:49

+0

數據庫表的編碼是什麼?確保它也是utf-8。同時檢查你的腳本是否收到正確的字符串在將表單數據插入到數據庫之前回顯表單數據。 (後驗證。)這裏 – 2013-02-20 10:01:58

+0

查找方案在MySQL SET NAMES UTF8?] [1] [1]:http://stackoverflow.com/questions/2159434/set-names-utf8-in- mysql – sanj 2013-02-20 10:04:40

回答

1

確保您與編碼UTF8創建數據庫

CREATE SCHEMA `youdatabasename` DEFAULT CHARACTER SET utf8; 
+0

以來,參數的順序已經顛倒了,這不是一個答案。 – 2013-02-20 10:03:47

+0

哦,這是我用它來確保我不會遇到編碼:)應該是一個評論呢? – 2013-02-20 10:06:57

+0

這是不夠的,以確保。 – 2013-02-20 10:11:00

2

嘗試編碼後插入數據。嘗試mysql_real_escape_string()進行編碼。然後執行插入查詢。

編輯: -

這個答案被張貼在一年前。現在mysql_real_escape_string() for php 5將工作於mysqli :: real_escape_string這種格式。請here

1

如果你不想擔心這麼多不同的字符集值編碼,或者ヶ輛不適合你的工作,這裏的選擇: 我用mysqli的數據庫連接(和PHPV5)表單POST寫作/插入到MySQl DB。

$Notes = $_POST['Notes']; //can be text input or textarea. 

$charset = mysqli_character_set_name($link); //only works for mysqli DB connection 
printf ("To check your character set but not necessary %s\n",$charset); 

$Notes = str_replace('"', '&quot;', $Notes); //double quotes for mailto: emails. 
$von = array("ä","ö","ü","ß","Ä","Ö","Ü"," ","é"); //to correct double whitepaces as well 
$zu = array("&auml;","&ouml;","&uuml;","&szlig;","&Auml;","&Ouml;","&Uuml;","&nbsp;","&#233;"); 
$Notes = str_replace($von, $zu, $Notes); 
echo " Notes:".$Notes."<br>" ; 
$Notes = mysqli_real_escape_string($link, $Notes); //for recommended mysqli DB connection. 
//$Notes = mysql_real_escape_string($Notes); //for old deprecated mysql DB connection. 

// mysqli_real_escape_string Escapes special characters in a string for use in an SQL statement 

echo " Notes:".$Notes ; //ready for inserting 
+0

顯示內容時:如果出現有趣的字符,則在標題中顯示此行 KarlosFontana 2014-01-03 21:45:55

+0

'mysqli_real_escape_string' should not be在那裏有一個完美的佔位符系統時使用。 – tadman 2014-04-10 04:44:01