2013-03-26 53 views
3

夥計。當我在西裏爾字母上輸入任何東西時,如:Цветана Пиронкова,然後單擊提交(進入表格),它將顯示它(並將其保存在mysql表格中),如下所示:Цветана Пиронкова 而且我不知道如何解決它。我認爲問題來自htmlspecialchars,但我不知道。這裏是我的索引文件:無法解決西里爾問題。 htmlspecialchars/PHP/MySQL

<?php // connect to the database 
     include('connect-db.php'); 

     // get results from database 
     $result = mysql_query("SELECT * FROM players") 
       or die(mysql_error()); 

mysql_query("SET NAMES UTF8"); 

     // display data in table 
     echo "<p><b>View All</b></p>"; 

     echo "<table class=\"table table-bordered table-hover\" border='1' cellpadding='10'>"; 
     echo "<tr> <th>ID</th> <th>Mqsto</th> <th>Ime</th> <th>Tochki</th> <th></th> <th></th></tr>"; 

     // loop through results of database query, displaying them in the table 
     while($row = mysql_fetch_array($result)) { 

       // echo out the contents of each row into a table 
       echo "<tr>"; 
       echo '<td>' . $row['id'] . '</td>'; 
       echo '<td>' . $row['mqsto'] . '</td>'; 
       echo '<td>' . $row['ime'] . '</td>'; 
       echo '<td>' . $row['tochki'] . '</td>'; 
       echo '<td><a href="editr.php?id=' . $row['id'] . '">Edit</a></td>'; 
       echo '<td><a href="deleter.php?id=' . $row['id'] . '">Delete</a></td>'; 
       echo "</tr>"; 
     } 

     // close table> 
     echo "</table>"; 
     ?> 
<p><a href="new.php">Add a new record</a></p><br><br> 

這裏是我的new.php文件:

<?php 
/* 
NEW.PHP 
Allows user to create a new entry in the database 
*/ 

// creates the new record form 
// since this form is used multiple times in this file, I have made it a function that is easily reusable 
function renderForm($mqsto, $ime, $tochki, $error) 
{ 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
<title>New Record</title> 
</head> 
<body> 
<?php 
// if there are any errors, display them 
if ($error != '') 
{ 
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; 
} 
?> 

<form action="" method="post"> 
<div> 
<strong>Mqsto: *</strong> <input type="text" name="mqsto" value="<?php echo $mqsto; ?>" /><br/> 
<strong>Ime: *</strong> <input type="text" name="ime" value="<?php echo $ime; ?>" /><br/> 
<strong>Tochki: *</strong> <input type="text" name="tochki" value="<?php echo $tochki; ?>" /><br/> 
<p>* required</p> 
<input type="submit" name="submit" value="Submit"> 
</div> 
</form> 
</body> 
</html> 
<?php 
} 




// connect to the database 
include('connect-db.php'); 

mysql_query("SET NAMES UTF8"); 

// check if the form has been submitted. If it has, start to process the form and save it to the database 
if (isset($_POST['submit'])) 
{ 
// get form data, making sure it is valid 
$mqsto = mysql_real_escape_string(htmlspecialchars($_POST['mqsto'])); 
$ime = mysql_real_escape_string(htmlspecialchars($_POST['ime'])); 
$tochki = mysql_real_escape_string(htmlspecialchars($_POST['tochki'])); 

// check to make sure both fields are entered 
if ($mqsto == '' || $ime == '' || $tochki == '') 
{ 
// generate error message 
$error = 'ERROR: Please fill in all required fields!'; 

// if either field is blank, display the form again 
renderForm($mqsto, $ime, $tochki, $error); 
} 
else 
{ 
// save the data to the database 
mysql_query("INSERT players SET mqsto='$mqsto', ime='$ime', tochki='$tochki'") 
or die(mysql_error()); 

// once saved, redirect back to the view page 
header("Location: ranglista.php"); 
} 
} 
else 
// if the form hasn't been submitted, display the form 
{ 
renderForm('','','',''); 
} 
?> 

回答

3

你有沒有設置好的的數據庫字符集爲UTF-8並在你的html中使用這個?

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

也請記住,UTF-8西里爾內部數據庫需要2個字節,因此當您設置VARCHAR或類似尺寸(如果你想顯示3000字符,你應該設定一個大小爲6000)注意

+0

如果您將數據庫表正確設置爲UTF-8,VARCHAR(3000)表示3000個字符,而不是3000個字節。它實際上會佔用9000個字節,因爲對於UTF-8,MySQL將每個字符使用3個字節。 – Evert 2013-03-27 18:38:04

+0

也許我錯了,但據我記得西里爾語是2個字節,也存儲字符數,但VARCHAR是基於字節大小(65530字節) – Razorphyn 2013-03-27 20:41:34

+0

http://dev.mysql.com/doc/ refman/5.0/en/char.html – Razorphyn 2013-03-27 20:45:30

3

添加到您的new.php文件的頂部

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