3

伙计们。当我在西里尔文上键入任何内容时,例如:Цветана Пиронкова,然后单击提交(到表中),它会显示它(并将其保存在 mysql 表中),就像这样:Цветана Пиронкова 而且我不知道如何修复它。我认为问题来自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('','','','');
 }
?> 
4

2 回答 2

3

添加到 new.php 文件的顶部

echo "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />";
于 2013-03-26T20:18:03.590 回答
3

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

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

还要记住,数据库中的 UTF-8 西里尔文需要 2 个字节,因此在设置 varchar 或类似大小时要注意(如果要显示 3000 个字符,则应设置大小为 6000)

于 2013-03-26T20:19:39.860 回答