5

我制作了一个用户可以填写的表格,它会检查用户是否正确插入了电子邮件。

  • 如果它返回 false:用户将看到一条错误消息。
  • 如果它返回 true:它将向用户显示一条确认消息,但它也会INSERT将数据存入数据库。

因此,当我尝试在 PHP 文件中添加表单时出现问题,以便我可以发出警报或在同一页面上向用户提供错误消息。我开始收到此错误:

未声明 HTML 文档的字符编码。如果文档包含 US-ASCII 范围之外的字符,则文档将在某些浏览器配置中呈现乱码。页面的字符编码必须在文档或传输协议中声明。

所以我试图通过恢复到旧版本来恢复它,我不得不剪切和粘贴几段代码,但没有太复杂。出于某种奇怪的原因,我仍然收到此错误。自然地,我在 Stackoverflow 上查看了关于这个问题的类似主题,但没有一个答案让我更进一步。例如meta在页面标题中添加信息,我还创建了一个这样的 PHP 标题:header('Content-type: text/html; charset=utf-8');之后我阅读了一些关于这个错误的文章,但没有一个帮助我解决这个问题。

这是我的代码示例。正是这个页面我得到了错误:

 <?php    
include_once('credentials.php');
mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
include_once('functions.php');

 $apples = sanitize($_POST['Apple']);
 $oranges = sanitize($_POST['Orange']);
 $melons = sanitize($_POST['Melon']);
 $pears = sanitize($_POST['Pear']);
 $strawberries = sanitize($_POST['Strawberry']);
 $grapes = sanitize($_POST['Grape']);               
    if($_POST['Grape'] == 'Yes')
    { $grapes = 1; }
    else
    { $grapes = 0; } 
$blueberries = sanitize($_POST['Blueberry']);

if (checkStrawberries($strawberries) == true) {    
    mysql_query("   INSERT INTO tablename (Name, Weight, Color, Ripeness, Age, Origin, Destination) 
            VALUES ('$apples', '$oranges', '$melons', '$Pears', '$strawberries', '$grapes', '$blueberries') ");

    or die(mysql_error()); 

    echo '<!DOCTYPE html>   
            <html>
                <head>
                    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
                </head>
                <title>Page</title>
                <body>
                    True
                    <a href="../index.php">home</a>
                </body>
            </html>';
} else {
    echo '<!DOCTYPE html>   
            <html>
                <head>
                    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
                </head>
                <title>Pagina</title>
                <body>
                    False
                    <a href="../index.php">home</a>
                </body>
            </html>';
}
?>

我不得不重命名变量和类,因为它们与我工作的公司关系密切。

编辑:此错误是由使用 Firebug 的 Firefox 中的控制台日志产生的。

编辑 2:更改了 var 和函数名称以避免混淆并说明此应用程序中的数据交换。

编辑 3:我试图弄清楚我的代码中究竟是什么产生了这个错误,因为我使用 Dobromir Velev 下面提供给我的测试用例测试了编码部分,并且效果很好。我决定注释掉 IF ELSE 语句并且我没有收到错误。所以这似乎破坏了页面。

我现在删除了回声,因为它没有为这个特殊的挑战增加任何价值。

if (checkStrawberries($strawberries) == true) {    
    mysql_query("INSERT INTO tablename (Name, Weight, Color, Ripeness, Age, Origin, Destination) 
    VALUES ('$apples', '$oranges', '$melons', '$Pears', '$strawberries', '$grapes', '$blueberries') ");
    or die(mysql_error());
} else {
    echo 'FALSE';
}

函数 checkStrawberries 是这样的:

function checkEmail($email) {
if (!preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" , $email)) {
return false;
}
return true;
}

清理功能是这样的:

function sanitize($dirty){
$clean = htmlspecialchars($dirty);
$cleaner = mysql_real_escape_string($clean);
return $cleaner;
}
4

2 回答 2

1

我会先尝试一个包含 HTTP 标头和元标记的文件。

<?php
header("Content-Type: text/html; charset=utf-8");
?>

<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
</head>
<body>
text
</body>
</html>

如果这不会触发错误,则可能是您的代码中的某些内容。例如,<?php标签前的空格会阻止 PHP 设置 HTTP 标头。此外,如果包含文件 file1.php 或 file2.php 返回任何内容,则可能会导致浏览器错误地检测到编码

于 2012-12-13T16:57:05.743 回答
1

经过对PHP聊天的一些讨论,问题似乎来自额外的分号

从 :

mysql_query(); or die();

到 :

mysql_query() or die();
于 2012-12-14T13:07:14.217 回答