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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题