Why do I keep getting an undefined index error even though var_dump() shows the index is defined?

StackOverflow https://stackoverflow.com/questions/13959944

  •  11-12-2021
  •  | 
  •  

Question

I'm creating an app to help keep track of the scholarship funds that kids in our youth ministry earn towards summer camp. This part of the app selects the amount they currently have from the database, saves it to a variable called $oldAmount, adds it to $fundsAmount, and updates the database with the new funds amount.

    //Select student's current allocated funds amount and save to $studentFunds array
    $selectQuery = "SELECT studentFunds FROM students WHERE studentNum = $studentNum";
    $selectStatement = $db -> prepare($selectQuery);
    $selectStatement -> execute();
    $studentFunds = $selectStatement -> fetchAll();

    //DEBUG: Display value of studentFunds array
    echo "Value of array studentFunds before operation: ";
    var_dump($studentFunds);

    //Save the value of $studentFunds['studentFunds'] to $oldAmount
    $oldAmount = $studentFunds['studentFunds'];

    //Perform operation: add old amount and new funds amount together
    $studentNewAmount = $oldAmount + $fundsAmount; 

    //DEBUG: display $studentNewAmount
    echo "Value of studentNewAmount after operation: ";
    echo $studentNewAmount;

    //DEBUG: $studentNewAmount = 255;
    $db -> query("UPDATE students SET studentFunds = '$studentNewAmount' WHERE studentNum = $studentNum");

For some reason, I keep getting this error whenever I run the app:

Notice: Undefined index: studentFunds in C:\xampp\htdocs\scholarshipManager\model\insertAllocation.php on line 31

Line 31 is here:

        $oldAmount = $studentFunds['studentFunds'];

The var_dump() displays the following contents for the $studentFunds array:

Value of array studentFunds before operation:

array(1) { 
    [0]=> array(2) { 
            ["studentFunds"]=> string(3) "200"
            [0]=> string(3) "200" 
    }
} 

Also, because of the error, my database is not being updated with the new amount.

As you can see, the studentFunds index does contain a value, so why is this happening. Am I misunderstanding the error or is there an error in my code?

Was it helpful?

Solution

Omar's answer is correct. For more detail, look at the documentation of fetchAll: PHP docs for fetchAll

The skinny is that fetchAll returns an indexed array of all rows from your query, and each if those indexed arrays contains an associative array of keys and values. As a result, the data you want is stored in $studentFunds[0]['studentFunds']

Since fetchAll returns an array, troubleshooting answers to questions like this should go much more quickly if you use print_r instead of var_dump. print_r's output formatting is much more suitable for picking apart (making sense of) arrays.

As a matter of practice, you should also wrap this into some form of sanity checking in case, for example, the passed-in $studentNum is invalid or not found in the database or if multiple records are found. Something like this:

$oldAmount = 0;
if( is_array( $studentFunds ) && count( $studentFunds ) == 1 )
{
   $oldAmount = $studentFunds[0]['studentFunds'];
}

You're asking for ugly errors, or worse, results from unexpected records to be printed to the interface if you fail to do at least basic sanity checking of the query results.

OTHER TIPS

$studentFunds is an array of one element. Try this on line 31:

 $oldAmount = $studentFunds[0]['studentFunds'];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top