theconfusedbroom
theconfusedbroom

Reputation: 57

PHP undefined index / variable

I get the following error with the below code:

Notice: Undefined variable: error in C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms and Databases\project.php on line 35

The code:

<?php 
    $clicked = false;
    if($clicked == false && isset($_POST['submit'])) {
        if ($_POST['label'] == '') {
            echo "<p>You must enter in a label!</p>";   
            $error = true;
        }
        if ($_POST['url'] == '') {
            echo "<p>You must enter in a url!</p>"; 
            $error = true;
        }
        if ($_POST['status'] == '') {
            echo "<p>You must enter in a status (0 or 1)!</p>"; 
            $error = true;  
        }
    }       
    if ($error != true) {
        if (isset($_POST['submit']) && $_POST['submit'] == '1'  ) {
            $query = "INSERT INTO nav (label, url, target, status, position) VALUES  ('$_POST[label]', '$_POST[url]', '$_POST[target]', $_POST[status], $_POST[position])"; 
            $result = mysqli_query($dbc, $query);
            if ($result) {
                echo "<p>You've added a new navigation link!</p>";
            }
            else {
                echo "<p>An error has occured!</p>"; 
                echo mysqli_error($dbc);
                echo '<p>'.$query.'</p>';
            }
            $clicked = true;//submit button replaced        
        }
    }           
?>          

<h1>Navigation</h1> 
<h5>*Required Fields</h5>
<form action="project.php" method="post">
    <label>*Label:</label>
    <p><input type="text" name="label" size="50" placeholder="Enter a Label" value=""/></p>         
    <label>Position:</label>
    <p><input type="text" name="position" size="10" placeholder="Enter a Position" value=""/></p>           
    <label>*URL:</label>
    <p><input type="text" name="url" size="50" placeholder="Enter a URL" value=""/></p>     
    <label>Target:</label>
    <p><input type="text" name="target" size="50" placeholder="Enter a target" value=""/></p>           
    <label>*Status:</label>
    <p><input type="text" name="status" size="10" value="" /></p>           

<?php 
    if ($clicked == false) {
        echo '<p><button type="submit" name="submit" value = "1" >Add Navigation Link</button></p>';
    }
    else {
        echo '<p><a href "project.php"  id = resetBtn>Do Another</a></p>';
    }   
?>

When I make the following changes:

if($clicked == false && $_POST['submit'] == "1") {
    if ($_POST['label'] == '') {
        echo "<p>You must enter in a label!</p>";   
        $error = true;
    }
    if ($_POST['url'] == '') {
        echo "<p>You must enter in a url!</p>"; 
        $error = true;
    }
    if ($_POST['status'] == '') {
        echo "<p>You must enter in a status (0 or 1)!</p>"; 
        $error = true;  
    }
}       

I get these errors:

Notice: Undefined index: submit in C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms and Databases\project.php on line 18

&

Notice: Undefined variable: error in C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms and Databases\project.php on line 35

So clearly the button of name "submit" isn't being "seen" for whatever reason; I believe. This makes some sense to me, kind of... If I'm not mistaken: php reads from start to finish in a linear sort of way, and since the form is below the if statement, the index does not exist yet. This I think is further corroborated by the fact that once I click the submit button, all errors disappear, and the if statement's error (echo) statements within the if statement, are thus executed.

This is mind boggling. This doesn't work either...

if(isset($_POST['submit']) && $_POST['submit'] == '1') {
    if (isset($_POST['label']) && $_POST['label'] == '') {
        echo "<p>You must enter in a label!</p>";   
        $error = true;
    }
    if (isset($_POST['url']) && $_POST['url'] == '') {
        echo "<p>You must enter in a url!</p>"; 
        $error = true;
    }
    if (isset($_POST['status']) && $_POST['status'] == '') {
        echo "<p>You must enter in a status (0 or 1)!</p>"; 
        $error = true;  
    }
}       

...YET, in a previous version of the code, the combo of isset and "is equal to" for an if-statement's conditions, solved the Unidentified index problem, as it pertained to the $_POST['submit'], here's that code: ps: as it pertains to this particular block of code, The fellow in the following linked tutorial, which I'm following along with, does not have any of these errors arise, despite me doing the exact same thing as him.

<?php 
    $clicked = false;
    if (isset($_POST['submit']) && $_POST['submit'] == '1'  ) {
        $query = "INSERT INTO nav (label, url, target, status, position) VALUES ('$_POST[label]', '$_POST[url]', '$_POST[target]', $_POST[status], $_POST[position])";  
        $result = mysqli_query($dbc, $query);
        if ($result) {
            echo "<p>You've added a new navigation link!</p>";}
        else {
            echo "<p>An error has occured!</p>"; 
            echo mysqli_error($dbc);
            echo '<p>'.$query.'</p>';
        }       
        $clicked = true;//submit button replaced        
    }   
?>          

<h1>Navigation</h1> 
<h5>*Required Fields</h5>   
<form action="project2.php" method="post">
    <label>*Label:</label>
    <p><input type="text" name="label" size="50" placeholder="Enter a Label" value=""/></p>
    <label>Position:</label>
    <p><input type="text" name="position" size="10" placeholder="Enter a Position" value=""/></p>
    <label>*URL:</label>
    <p><input type="text" name="url" size="50" placeholder="Enter a URL" value=""/></p>
    <label>Target:</label>
    <p><input type="text" name="target" size="50" placeholder="Enter a target" value=""/></p>
    <label>*Status:</label>
    <p><input type="text" name="status" size="10" value="" /></p>

<?php 
    if ($clicked == false) {
        echo '<p><button type="submit" name="submit" value = "1" >Add Navigation Link</button></p>';
    }
    else {
        echo '<p><a href "project2.php"  id = resetBtn>Do Another</a></p>';
    }   
?>

again, this works just fine, no errors. So why do I get the undefine variable error in the first block of code I posted? The undefined variable is coming as a result of the subsequent if-statements failing to be executed, and that is I'm assuming a problem to do with the index issue, yet, the errors do not reflect that!

When I replace the conditions with just $clicked == false, as follows:

$clicked = false;
if($clicked == false) {
    if ($_POST['label'] == '') {
        echo "<p>You must enter in a label!</p>";   
         $error = true;
    }
    if ($_POST['url'] == '') {
        echo "<p>You must enter in a url!</p>"; 
        $error = true;
    }
    if ($_POST['status'] == '') {
        echo "<p>You must enter in a status (0 or 1)!</p>"; 
        $error = true;  
    }
}       

I get these three undefined index errors ALONG WITH THE BLOODY CODE which obviuosly executes successfully, despite the three indexes being supposed undefined:

Notice: Undefined index: label in C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms and Databases\project4.php on line 20 You must enter in a label!

Notice: Undefined index: url in C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms and Databases\project4.php on line 24 You must enter in a url!

Notice: Undefined index: status in C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms and Databases\project4.php on line 28 You must enter in a status (0 or 1)!

Upvotes: 0

Views: 2359

Answers (2)

Medvech
Medvech

Reputation: 36

You need to define $error variable before first IF statement

$error = true;

because PHP processor doesn't know anything about it

Also in the code nothing about defined database connection, you need to have defined $dbc variable

$dbc = mysqli_connect("localhost","my_user","my_password","my_db");

Upvotes: 2

Daryl Gill
Daryl Gill

Reputation: 5524

There is alot of exampled code, which I haven't looked all the way through, but.. Long story short, an explanation of your errors are as followed:

Notice: Undefined variable

Your variable is unintialized when you're trying to access/use it.

echo $_HaveNotSetThisVar; // as this is not set prior to using. You're getting an error

and

Notice: Undefined index:

Much like your previous error. This time in an array form:

echo $Array['NonExistantKey']; // Undefined index 

A solution to this would be wrapping variables which might not always be set and a form or error handling:

http://php.net/isset


Practical usage:

if (isset($_HaveNotSetThisVar)){
  echo "Variable Set";
}

and the same for arrays:

if (isset($Array['NonExistantKey'])){
   echo "Array Key Set";
}

This is the basic of error handling, but can be stacked with other functions for more in-depth handling.

P.S

Another thing to ensure, is that you are using the correct variable names, undefined indexes/variables can also be caused by typos:

 $Vaar = "Test";
 echo $Var; // Undefined Error

Upvotes: 0

Related Questions