I made a form that users can fill out and it checks if the user correctly inserts the email.
INSERT
the data into the database.So the problem occurred when I tried to add the form in the PHP file so I could make an alert or give the user the error message on the same page. I started to get this error:
The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must to be declared in the document or in the transfer protocol.
So I tried to restore this by reverting to the older version, I had to cut and paste a few pieces of code but nothing too complicated. For some strange reason I still get this error. Naturally I checked the similar topics on this issue here on Stackoverflow but none of the answers got me any further. For example adding the meta
information in the header of the page,I also created a PHP header like this: header('Content-type: text/html; charset=utf-8');
After that I read some articles about this error but none helped me resolve this issue.
Here is an example of my code. It is this page I get the error on:
<?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>';
}
?>
I had to rename the variables and classes as they have strong relations with the company I work for.
EDIT: This error is being produced by the console log in Firefox using Firebug.
EDIT 2: Changed the var and function names to avoid confusion and to illustrate the exchange of data within this application.
EDIT 3: I tried to figure out what exactly in my code is producing this error, because I tested the encoding part with the test case presented to me below by Dobromir Velev and that worked just fine. I decided to comment out the IF ELSE statement and I didnt get an error. So this seems to corrupt the page.
I removed the echo's for now as it didn't add any value to this particular challenge.
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';
}
The function checkStrawberries is this:
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;
}
The function sanitize is this:
function sanitize($dirty){
$clean = htmlspecialchars($dirty);
$cleaner = mysql_real_escape_string($clean);
return $cleaner;
}
After some discussion on the PHP chat, it seems the problem comes from the extra semicolon
From :
mysql_query(); or die();
To :
mysql_query() or die();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With