html-encode输出&&字符串错误

时间:2010-04-08 09:57:49

标签: php mysql html database character-encoding

我的数据包括阿拉伯字符,它在mysql中看起来像垃圾,但在浏览器上运行时显示正确。我的问题:

  • 我如何对输出进行html编码?
  • 如果我将其添加到我的所有文件中:<meta http-equiv="Content-Type" content="text/html; charset=utf-8">我收到此错误:Error: Incorrect string value: '\xE4\xEE\xC3\xD8\xEF\xE6...' for column 'cQuotes' at row 1

我正在使用php / mysql平台。

在html中插入表格:

<!doctype html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Your Favorite Quotes</title>
<link rel="stylesheet" type="text/css" href="style.css" /> 
<link rel="stylesheet" href="css/validationEngine.jquery.css" type="text/css" media="screen" charset="utf-8" />
<script type="text/javascript" src="scripts/jquery-1.4.2.js"></script>
<script src="scripts/jquery.validationEngine-en.js" type="text/javascript"></script> 
<script src="scripts/jquery.validationEngine.js" type="text/javascript"></script>
<script type="text/javascript">
            $(document).ready(function() { 
                $("#submitForm").validationEngine() 
            })  
</script>
</head>
<body>

<div class="container">
<div class="center_div">
<h2>Submit Your Quote</h2>
<fieldset>
<form id="submitForm" action="qinsert.php" method="post">
<div class="field">
<label>Author: </label>
<input id="author" name="author" type="text" class="validate[required,custom[onlyLetter],length[0,100]]">
</div><br />
<div class="field">
<label>Quote: </label>
<textarea id="quote" name="quote" class="validate[required, length[0,1000]]"></textarea>
<br />
</div>
<input id="button1" type="submit" value="Submit" class="submit" /><br />
<input id="button2" type="reset" value="Reset" /> 
</form>
</fieldset>
</div>
</div>


</body>
</html>

//////////////////////

在php中查询:

//<?php
//header('Content-Type: text/html; charset=UTF-8');
//?>
<!doctype html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="style2.css" /> 
<title>Your Quote Databank</title>
</head>
<body>

<?php
include 'config.php';

echo "Connected <br />";


//check for quotes and apostrophes

$author = '';
$quote = '';

$author = $_POST['author'];
$quote = $_POST['quote'];

$author = mysql_real_escape_string(trim($author)); 
$quote = mysql_real_escape_string(trim($quote)); 


//**************************


//validating data

$query = "SELECT * FROM Quotes where cQuotes = '$quote' limit 1;";

$result = mysql_query($query, $conn);

//now check that the number of rows is 0

if (mysql_num_rows($result) > 0 ) {
header("Location: /error.html");
exit;
}



//inserting data
//mysql_query("SET NAMES 'utf8'");
//mysql_query("SET CHARACTER SET utf8");
$sql="INSERT INTO Quotes (vauthor, cquotes)
VALUES ('$author', '$quote')";

if (!mysql_query($sql,$conn))
  {
  die('Error: ' . mysql_error());
  }
echo "<div class='container'><p><label class='lbl_record'> Record Added Successfully!</label>";
echo "<a href='qform.html'> Submit a New Quote!</a></p>";

//**************************


//selecting data
$result = mysql_query("SELECT * FROM Quotes ORDER BY idQuotes DESC");

echo "<div class='center_div'>";
echo "<table>
<thead>
<tr>
<th>Author</th>
<th>Quotes</th>
</tr>
</thead>";

while($row = mysql_fetch_array($result))
  {
  echo "<tbody><tr>";
  echo "<td width='150px'>" . $row['vAuthor'] . "</td>";
  echo "<td>" . $row['cQuotes'] . "</td>";
  echo "</tr>";
  }
echo "</tbody></table>";
echo "</div></div>";
//**************************

include 'close_config.php';

?>

</body>
</html>

5 个答案:

答案 0 :(得分:2)

没有“人物”这样的东西。只有编码。您必须确定您正在使用的编码。然后为应用程序的所有三个部分设置此编码:

  • HTML页面
  • PHP脚本
  • 数据库

强烈建议使用UTF-8 要使用它,您必须通过创建具有default charset=utf8的表,mysql客户端编码,在客户端应用程序中执行SET NAMES utf8查询,以及为页面设置header("Content-Type: text/html; charset=utf-8");来设置数据库编码包含html表单和包含结果的页面

根据您的版本:
在config.php文件中添加以下行:

mysql_query("SET NAMES utf8");

还要检查mysql表编码。您可以使用show create table Quotes查询

执行此操作

答案 1 :(得分:2)

Incorrect string value: '\xE4\xEE\xC3\xD8\xEF\xE6...'

看起来像是用ISO-8859-6编码的阿拉伯字符串。如果您收到ISO-8859-6字节字符串并尝试将其插入UTF-8数据库表,则会收到此错误。

如果您的表单页面被正确标记为UTF-8,则您的脚本不应该从浏览器收到ISO-8859-6,因为meta意味着。在浏览器中检查显示表单时,View-&gt;编码菜单勾选了“UTF-8”。传回真实<meta>标题的Web服务器可能会覆盖Content-Type: text/html;charset=...

如果您的PHP尝试使用错误的字符集与服务器通信,也可能发生这种情况。我看到你已经评论了SET NAMES ...我会优先使用mysql_set_charset ('utf8');

echo "<td>" . $row['cQuotes'] . "</td>";

您需要在此处对输出进行HTML编码,而不是因为字符集问题。任何<&字符都需要在文本中进行编码,否则它们会注入不需要的HTML标记,包括JavaScript,在这种情况下,您会遇到跨站点脚本问题。

echo '<td>'.htmlspecialchars($row['cQuotes']).'</td>';

除此之外:...然而,PHP是一种模板语言。使用它,不要试图自己进行字符串模板化。

<table>
    <?php while ($row= mysql_fetch_array($result)) { ?>
        <tr>
            <td width="150"><?php h($row['vAuthor']) ?></td>
            <td><?php h($row['cQuotes']) ?></td>
        </tr>
    <?php } ?>
</table>

width="150px"不起作用,px仅适用于CSS。)上面假设这样的辅助函数可以阻止你输入htmlspecialchars这么多:

function h($s) {
    echo htmlspecialchars($s, ENT_QUOTES);
}

答案 2 :(得分:0)

您不需要使用HTML实体。您需要选择适合阿拉伯字符的字符编码(例如UTF-8)并使用所有中间工具正确使用它:MySQL,mysql_query(),PHP,HTML ......

答案 3 :(得分:0)

试试这个:

while($row = mysql_fetch_array($result))
{
  echo "<tbody><tr>";
  echo "<td width='150px'>" . htmlentities($row['vAuthor']) . "</td>";
  echo "<td>" . htmlentities($row['cQuotes']) . "</td>";
  echo "</tr>";
}

有关详细信息,请参阅htmlentities函数参考

答案 4 :(得分:-1)

当您使用jquery验证时,您可以尝试使用阿拉伯语

"onlyLetterSp": {
    "regex": /^[a-zA-Z\u0600-\u06FF\ \']+$/,
    "alertText": "* أحرف فقط"
},