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?