无法解决西里尔字母问题。htmlspecialchars/PHP/MySQL

3

大家好。当我输入任何西里尔字母,如:Цветана Пиронкова,并点击提交(到表格中),它会显示并保存在mysql表格中,像这样:Цветана Пиронкова 我不知道如何解决这个问题。我认为问题出在htmlspecialchars上,但我不确定。这是我的index文件:

<?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('','','','');
 }
?> 
2个回答

3
请将以下代码添加到您的new.php文件顶部:
echo "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />";

3

您是否将数据库字符集设置为UTF-8并在html中使用了它?

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

请注意,在数据库中使用的UTF-8西里尔字母占用2个字节,因此在设置varchar或类似大小时要特别注意(如果您想显示3000个字符,则应设置为6000)。


如果您正确地将数据库表设置为UTF-8,VARCHAR(3000)表示3000个字符,而不是3000个字节。实际上,它将占用9000个字节,因为对于UTF-8,MySQL将使用每个字符3个字节。 - Evert
也许我记错了,但据我所知,Cyrillic是2个字节,而CHAR存储字符数,而VARCHAR基于字节大小(65530字节)。 - Razorphyn
Cyrillic 在 UTF-8 中可能只需要 2 个字节,但 MySQL 实际上并不将数据存储为 UTF8,更像是一种可以称之为 UCS-3 的格式 :). 如果该列标记为 UTF-8,则每个字符始终占用 3 个字节。UTF-8 对于索引和存储来说很糟糕,因此他们选择了一种固定大小的字符格式。最大限制确实是以字节为单位的,但括号中的任何内容都不是。 - Evert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接