Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do I keep getting an undefined index error even though var_dump() shows the index is defined?
    primarykey
    data
    text
    <p>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.</p> <pre><code> //Select student's current allocated funds amount and save to $studentFunds array $selectQuery = "SELECT studentFunds FROM students WHERE studentNum = $studentNum"; $selectStatement = $db -&gt; prepare($selectQuery); $selectStatement -&gt; execute(); $studentFunds = $selectStatement -&gt; 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 -&gt; query("UPDATE students SET studentFunds = '$studentNewAmount' WHERE studentNum = $studentNum"); </code></pre> <p>For some reason, I keep getting this error whenever I run the app:</p> <blockquote> <p>Notice: Undefined index: studentFunds in C:\xampp\htdocs\scholarshipManager\model\insertAllocation.php on line 31</p> </blockquote> <p>Line 31 is here:</p> <pre><code> $oldAmount = $studentFunds['studentFunds']; </code></pre> <p>The var_dump() displays the following contents for the $studentFunds array:</p> <p>Value of array studentFunds before operation: </p> <pre><code>array(1) { [0]=&gt; array(2) { ["studentFunds"]=&gt; string(3) "200" [0]=&gt; string(3) "200" } } </code></pre> <p>Also, because of the error, my database is not being updated with the new amount.</p> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload