Undefined Index Error PHP

Undefined index error PHP

Try:

<?php

if (isset($_POST['name'])) {
$name = $_POST['name'];
}

if (isset($_POST['price'])) {
$price = $_POST['price'];
}

if (isset($_POST['description'])) {
$description = $_POST['description'];
}

?>

Problem with A PHP Error was encountered : Undefined Index

The $post variable doesn't contain, 'nik' key in it.

Undefined index means that you array doesn't contain that 'key' in it.

So please dump(var_dump($post) or print_r($post)) your $post variable first, to see what data comes.

You can see the Controllers documentation here.

And Request documentation here.

How to resolve Undefined index error in PHP

try below where you are displaying the menu

<nav>
<a id="mainpage">Main Page</a>
<?php if (!isset($_SESSION['logged_in'])) { ?>
<a href="login2.php">Login</a>
<a href="register.php">Register</a>
<?php } else { ?>
<a href="post.php">Posting</a>
<a href="#">Members posts</a>
<a href="logout.php" class="outbutton">Logout</a>
<?php } ?>

Errors: PHP Notice: Undefined index

This is not dangerous for your site per se. The variable(s) in the GET are not set (variables $_GET['url'] is not set). If you plan to use it later in your PHP script, you should set them before, on the previous page. So, there are two pages, first one and your PHP page - the second one.

undefined index error in php and How to fix it

The error occurs at the first page opening because at that point the user haven't already pressed the form's submit button.

So, use this code:

if(!empty($_POST['finish']))
{
if(isset($_POST['payment']))
{
$payment = mysqli_real_escape_string($conn,$_POST["payment"]);

if($_POST['payment'] == "skippayment")
{
echo "payment skipped";
}
}
else echo "payment empty";
}

What part of my code is causing an Undefined Index error? (PHP)

$_GET is an array, and if the user haven't defined a scid=something in the query string, then $_GET['scid'] is undefined. There, you got an undefined index Notice.

You should test if isset($_GET['scid']) before trying to read it's value.

PHP tutorial, issues with Undefined index and recommended isset fixes

You have to check isset before you assign it to variable. change your php script as below:

<?php

if(isset($_GET['name']) && isset($_GET['age']) && !empty($_GET['name']) && !empty($_GET['age'])) {
$name = $_GET['name'];
$age = $_GET['age'];
echo 'I am '.$name.' and I am '.$age.' years of age.';
} else {
echo 'Please type something.';
}

?>

Notice: Undefined variable, Notice: Undefined index, Warning: Undefined array key, and Notice: Undefined offset using PHP

Notice / Warning: Undefined variable

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 turned 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. Additionally and more ideal is the solution of empty() since it does not generate a warning or error message if the variable is not initialized.

From PHP documentation:

No warning is generated if the variable does not exist. That means
empty() is essentially the concise equivalent to !isset($var) || $var
== false
.

This means that you could use only empty() to determine if the variable is set, and in addition it checks the variable against the following, 0, 0.0, "", "0", null, false or [].

Example:

$o = [];
@$var = ["",0,null,1,2,3,$foo,$o['myIndex']];
array_walk($var, function($v) {
echo (!isset($v) || $v == false) ? 'true ' : 'false';
echo ' ' . (empty($v) ? 'true' : 'false');
echo "\n";
});

Test the above snippet in the 3v4l.org online PHP editor

Although PHP does not require a 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 will be used 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, for example when you try to append a string to an undefined variable. Or use isset() / !empty() to check if they are declared before referencing them, as in:

    //Initializing variable
    $value = ""; //Initialization value; Examples
    //"" When you want to append stuff later
    //0 When you want to add numbers later
    //isset()
    $value = isset($_POST['value']) ? $_POST['value'] : '';
    //empty()
    $value = !empty($_POST['value']) ? $_POST['value'] : '';

This has become much cleaner as of PHP 7.0, now you can use the null coalesce operator:

    // Null coalesce operator - No need to explicitly initialize the variable.
$value = $_POST['value'] ?? '';

  1. 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)
  2. Disable E_NOTICE from reporting. A quick way to exclude just E_NOTICE is:

    error_reporting( error_reporting() & ~E_NOTICE )
  3. Suppress the error with the @ operator.

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

Notice: Undefined index / Undefined offset / Warning: Undefined array key

This notice/warning appears when you (or PHP) try to access an undefined index of an array.

Ways to deal with the issue:

  1. Check if the index exists before you access it. For this you can use isset() or array_key_exists():

    //isset()
    $value = isset($array['my_index']) ? $array['my_index'] : '';
    //array_key_exists()
    $value = array_key_exists('my_index', $array) ? $array['my_index'] : '';
  2. The language construct list() may generate this when it attempts to access an array index that does not exist:

    list($a, $b) = array(0 => 'a');
    //or
    list($one, $two) = explode(',', 'test string');

Two variables are used to access two array elements, however there is only one array element, index 0, so this will generate:

Notice: Undefined offset: 1

#$_POST / $_GET / $_SESSION variable

The notices above appear often when working with $_POST, $_GET or $_SESSION. For $_POST and $_GET you just have to check if the index exists or not before you use them. For $_SESSION you have to make sure you have the session started with session_start() and that the index also exists.

Also note that all 3 variables are superglobals and are uppercase.

Related:

  • Notice: Undefined variable
  • Notice: Undefined Index

PHP Undefined index: error on Dropdown post request

You missed the name attribute in your select element.

Add a name to the select element like this:

echo '<select class="form-control" id="sel1" name="faculty_id">';

You are getting the post request by using your form post method on second page. So use the name attribute on that page with $_POST super global with given name of the field: $_POST['faculty_id']



Related Topics



Leave a reply



Submit