dongtan1845 2012-12-19 19:43
浏览 68
已采纳

为什么即使var_dump()显示索引已定义,我仍然会收到未定义的索引错误?

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?

  • 写回答

2条回答 默认 最新

  • douluozhan4370 2012-12-19 20:06
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler