Can't update data in to mysql through php [duplicate]

ساخت وبلاگ

Vote count: 416

I am running a PHP script, and keep getting errors like:

Undefined variable: user_location in C:wampwwwmypathindex.php on line 12

Line 12 looks like this:

$greeting = "Hello, ".$user_name." from ".$user_location;

What do these errors mean?

Why do they appear all of a sudden? I used to use this script for years and I've never had any problem.

What do I need to do to fix them?

Is there a quick fix?

This is a General Reference question for people to link to as duplicate, instead of having to explain the issue over and over again. I feel this is necessary because most real-world answers on this issue are very specific.

Related Meta discussion:

community wiki

14 Answers

Vote count: 422 accepted

From the vast wisdom of the PHP Manual:

Relying on the default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name. It is also a major security risk with register_globals tued on. E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array. isset() language construct can be used to detect if a variable has been already initialized.

Some explanations:

Although PHP does not require variable declaration, it does recommend it in order to avoid some security vulnerabilities or bugs where one would forget to give a value to a variable that he will use later in the script. What PHP does in the case of undeclared variables is issue a very low level error, E_NOTICE, one that is not even reported by default, but the Manual advises to allow during development.

Ways to deal with the issue:

  1. Recommended: Declare your variables. Or use isset() to check if they are declared before referencing them, as in: $value = isset($_POST['value']) ? $_POST['value'] : '';.

  2. Use empty(), so no waing is generated if the variable does not exist. It's equivalent to !isset($var) || $var == false. E.g.

    echo "Hello " . (!empty($user) ? $user : "");
  3. Set a custom error handler for E_NOTICE and redirect the messages away from the standard output (maybe to a log file). set_error_handler('myHandlerForMinorErrors', E_NOTICE | E_STRICT).

  4. Disable E_NOTICE from reporting. A quick way to exclude just E_NOTICE is error_reporting( error_reporting() & ~E_NOTICE ).

  5. Suppress the error with the @ operator.

Note: It's strongly recommended to implement just point 1.

Related:

community wiki

Vote count: 48

Try these

Q1: this notice means $vaame is not defined at current scope of the script.

Q2: Use of isset(), empty() conditions before using any suspicious variable works well.

// recommended solution
$user_name = $_SESSION['user_name'];
if (empty($user_name)) $user_name = '';
OR
// just define at the top of the script index.php
$user_name = '';
$user_name = $_SESSION['user_name'];
OR
$user_name = $_SESSION['user_name'];
if (!isset($user_name)) $user_name = '';

QUICK Solution:

// not the best solution, but works
// in your php setting use, it helps hiding site wide notices
error_reporting(E_ALL ^ E_NOTICE);

Note about sessions:

community wiki

Vote count: 19

A (often discouraged) alteative is the error suppression operator @. It is a specific language construct to shut down undesired notices and waings, but should be used with care.

First, it incurs a microperformance penalty over using isset. That's not measurable in real world applications, but should be considered in data heavy iterations. Secondly it might obstruct debugging, but at the same time suppressed errors are in fact passed on to custom error handlers (unlike isset decorated expressions).

community wiki

Vote count: 15

It means you are testing, evaluating, or printing a variable that you have not yet assigned anything to. It means you either have a typo, or you need to check that the variable was initialized to something first. Check your logic paths, it may be set in one path but not in another.

community wiki

Vote count: 12

Generally because of "bad programming", and a possibility for mistakes now or later.

  1. If it's a mistake, make a proper assignment to the variable first: $vaame=0;
  2. If it really is only defined sometimes, test for it: if (isset($vaame)) .... before using it
  3. If it's because you spelled it wrong, just correct that
  4. Maybe even tu of the waings in you PHP-settings
community wiki

Vote count: 10

I didn't want to disable notice because it's helpful, but wanted to avoid too much typing.

My solution was this function:

function ifexists($vaame)
{ retu(isset($$vaame)?$vaame:null);
}

So if I want to reference to $name and echo if exists, I simply write:

<?=ifexists('name')?>

For array elements:

function ifexistsidx($var,$index)
{ retu(isset($var[$index])?$var[$index]:null);
}

In page if I want to refer to $_REQUEST['name']:

<?=ifexistsidx($_REQUEST,'name')?>
community wiki

Vote count: 8

The best way for getting input string is:

$value = filter_input(INPUT_POST, 'value');

This one-liner is almost equivalent to:

if (!isset($_POST['value'])) { $value = null;
} elseif (is_array($_POST['value'])) { $value = false;
} else { $value = $_POST['value'];
}

If you absolutely want string value, just like:

$value = (string)filter_input(INPUT_POST, 'value');
community wiki

Vote count: 7

Its because the variable '$user_location' is not getting defined. If you are using any if loop inside which you are declaring the '$user_location' variable then you must also have an else loop and define the same. For example:

$a=10;
if($a==5) { $user_location='Paris';} else { }
echo $user_location;

The above code will create error as The if loop is not satisfied and in the else loop '$user_location' was not defined. Still PHP was asked to echo out the variable. So to modify the code you must do the following:

$a=10;
if($a==5) { $user_location='Paris';} else { $user_location='SOMETHING OR BLANK'; }
echo $user_location;
community wiki

Vote count: 5

I used to curse this error, but it can be helpful to remind you to escape user input.

For instance, if you thought this was clever, shorthand code:

// Echo whatever the hell this is
<?=$_POST['something']?>

...Think again! A better solution is:

// If this is set, echo a filtered version
<?=isset($_POST['something']) ? html($_POST['something']) : ''?>

(I use a custom html() function to escape characters, your mileage may vary)

community wiki

Vote count: 4

the quick fix is to assign your variable to null at the top of your code

$user_location = null;
community wiki

Vote count: 3

I use all time own useful function exst() which automatically declare variables.

Your code will be -

$greeting = "Hello, ".exst($user_name, 'Visitor')." from ".exst($user_location);
/** * Function exst() - Checks if the variable has been set * (copy/paste it in any place of your code) * * If the variable is set and not empty retus the variable (no transformation) * If the variable is not set or empty, retus the $default value * * @param mixed $var * @param mixed $default * * @retu mixed */
function exst( & $var, $default = "")
{ $t = ""; if ( !isset($var) || !$var ) { if (isset($default) && $default != "") $t = $default; } else { $t = $var; } if (is_string($t)) $t = trim($t); retu $t;
}
community wiki

Vote count: 3

In a very Simple Language.
The mistake is you are using a variable $user_location which is not defined by you earlier and it doesn't have any value So I recommend you to please declare this variable before using it, For Example:


$user_location = '';
Or
$user_location = 'Los Angles';
This is a very common error you can face.So don't worry just declare the variable and Enjoy Coding.
community wiki

Vote count: -1

why not keep things simple?

<?php
error_reporting(E_ALL); // making sure all notices are on
function idxVal(&$var, $default = null) { retu empty($var) ? $var = $default : $var; }
echo idxVal($arr['test']); // retus null without any notice
echo idxVal($arr['hey ho'], 'yo'); // retus yo and assigns it to array index, nice
?>
community wiki

Vote count: -1

Short Tag gotcha.

Included a file having a variable & using the variable in calling file. The Notice will occur $variable is undefined.

The reason for that was is short tags are disabled and the code instead of would not work.

The code would never executed in short tags hence the undefined variable error/notice. Disabled by default in php7

community wiki

back soft...
ما را در سایت back soft دنبال می کنید

برچسب : نویسنده : استخدام کار backsoft بازدید : 382 تاريخ : شنبه 29 اسفند 1394 ساعت: 20:41