PHP is writing this error in the logs: "Notice: Use of undefined constant".
Error in logs:
PHP Notice: Use of undefined constant department - assumed 'department' (line 5)
PHP Notice: Use of undefined constant name - assumed 'name' (line 6)
PHP Notice: Use of undefined constant email - assumed 'email' (line 7)
PHP Notice: Use of undefined constant message - assumed 'message' (line 8)
Relevant lines of code:
$department = mysql_real_escape_string($_POST[department]);
$name = mysql_real_escape_string($_POST[name]);
$email = mysql_real_escape_string($_POST[email]);
$message = mysql_real_escape_string($_POST[message]);
What does it mean and why am I seeing it?
You should quote your array keys:
$department = mysql_real_escape_string($_POST['department']);
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$message = mysql_real_escape_string($_POST['message']);
As is, it was looking for constants calleddepartment
,name
,email
,message
, etc. When it doesn't find such a constant, PHP (bizarrely) interprets it as a string ('department', etc). Obviously, this can easily break if you do defined such a constant later (though it's bad style to have lower-case constants).
The error message is due to the unfortunate fact that PHP will implicitly declare an unknown token as a constant string of the same name.
That is, it's trying to interpret this (note the missing quote marks):
{-code-1}
The only valid way this would be valid syntax in PHP is if there was previously a constant{-code-2}
defined. So sadly, rather than dying with a Fatal error at this point, it issues this Notice and acts as though a constant had been defined with the same name and value:
// Implicit declaration of constant called {-code-2} with value '{-code-2}'
define('{-code-2}', '{-code-2}');
There are various ways you can get this error message, but they all have the same root cause - a token that could be a constant.
{-code-4}
This is what the problem is in your case, and it's because you've got string array keys that haven't been quoted. Fixing the string keys will fix the bug:
Change:
{-code-8}{-code-2} = mysql_real_escape_string({-code-1});
...(etc)...
To:
{-code-8}{-code-2} = mysql_real_escape_string({-code-8}_POST['{-code-2}']);
...(etc)...
{-code-7}
Another reason you might see this error message is if you leave off the{-code-8}
from a variable, or{-code-8}this->
from a member. Eg, either of the following would cause a similar error message:
my_local; // should be {-code-8}my_local
my_member; // should be {-code-8}this->my_member
{-code-8}bad-variable-name
A similar but more subtle issue can result if you try to use a disallowed character in a variable name - a hyphen (-
) instead of an underscore_
would be a common case.
For example, this is OK, since underscores are allowed in variable names:
if (123 === {-code-8}my_var) {
do_something();
}
But this isn't:
if (123 === {-code-8}my-var) {
do_something();
}
It'll be interpreted the same as this:
if (123 === {-code-8}my - var) { // variable {-code-8}my minus constant 'var'
do_something();
}
In order to refer to a class constant you need to specify the class scope with::
, if you miss this off PHP will think you're talking about a globaldefine()
.
Eg:
class MyClass {
const MY_CONST = 123;
public function my_method() {
return self::MY_CONST; // This is fine
}
public function my_method() {
return MyClass::MY_CONST; // This is fine
}
public function my_bad_method() {
return MY_CONST; // BUG - need to specify class scope
}
}
There are some system-defined constants that only exist in newer versions of PHP, for example the mode option constants for such as
PHP_ROUND_HALF_DOWN
only exist in PHP 5.3 or later.
So if you tried to use this feature in PHP 5.2, say:
{-code-8}rounded = round({-code-8}my_var, 0, PHP_ROUND_HALF_DOWN);
You'd get this error message:
Use of undefined constant PHP_ROUND_HALF_DOWN - assumed 'PHP_ROUND_HALF_DOWN' Warning (2): Wrong parameter count for round()
you probably forgot to use""
.
For exemple:
$_array[text] = $_var;
change to:
$_array["text"] = $_var;
You missed putting single quotes around your array keys:
$_POST[email]
should be:
$_POST['email']
The correct way of using post variables is
<?php
$department = $_POST['department'];
?>
Use single quotation(')
<?php
${test}="test information";
echo $test;
?>
Notice: Use of undefined constant test - assumed 'test' in D:\xampp\htdocs\sp\test\envoirnmentVariables.php on line 3 test information
Insert single quotes.
Example
$department = mysql_real_escape_string($_POST['department']);
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$message = mysql_real_escape_string($_POST['message']);
Am not sure if there is any difference am using code igniter and i use "" for the names and it works great.
$department = mysql_real_escape_string($_POST["department"]);
$name = mysql_real_escape_string($_POST["name"]);
$email = mysql_real_escape_string($_POST["email"]);
$message = mysql_real_escape_string($_POST["message"]);
regards,
Jorge.
Looks like the predefined fetch constants went away with the MySQL extension, so we need to add them before the first function...
//predifined fetch constants
define('MYSQL_BOTH',MYSQLI_BOTH);
define('MYSQL_NUM',MYSQLI_NUM);
define('MYSQL_ASSOC',MYSQLI_ASSOC);
I tested and succeeded.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.