微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法解决西里尔问题.用htmlspecialchars / PHP / MySQL的

家伙.当我在西里尔字母上键入任何内容时:ЦветанаПиронкова,然后单击提交(进入表格),它会显示它(并将其保存在mysql表格中),如:& amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; ;&放大器;放大器;#1077;&放大器;放大器;#1090;&放大器;放大器;#1072;&放大器;放大器;#1085;&放大器;放大器;#1072; &放大器;放大器;#1055;&放大器;放大器;#1080;&放大器;放大器;#1088;&放大器;放大器;#1086;&放大器;放大器;#1085;&放大器;放大器;#1082;&放大器;放大器;#1086;&安培;放大器;#1074;&放大器;放大器;#1072;
我没有任何想法如何解决它.我认为问题来自htmlspecialchars,但我不知道.这是我的索引文件

<?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('','','','');
 }
?> 

解决方法:

你有没有将数据库charset设置为UTF-8并在你的html中使用它?

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

还记得UTF-8西里尔文内部数据库占用2个字节,所以在设置varchar或类似大小时要注意(如果要显示3000个字符,则应设置大小为6000)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐