注意:未定义的偏移php

时间:2015-08-06 15:48:53

标签: php notice

我有这个 php 代码,它会抛出警告notice: undefined offset

$thisMonth=$_POST['month']; //the value is today's date's month
$thisYear=$_POST['year']; //the value is today's date's year
$thisDay=$_POST['day']; //the value is today's date's day

$table=mysql_query("SELECT * FROM `kids` WHERE `debt`!='0'") or die(mysql_error());
$debt=0;

while($row=mysql_fetch_assoc($table)){
    $explodedDate=explode('/',$row['enrollmentdate']);
    $theYear=$explodedDate[0];
    $theMonth=$explodedDate[1]; //this line throws the error
    $theDay=$explodedDate[2]; //and also this line
    if((int)$theYear==(int)$thisYear && (int)$theMonth==(int)$thisMonth){
        if((int)$theDay==(int)$thisDay || (int)$thisDay==0){
            $debt+=$row['debt'];
            }
        }
    }

我一直在网上阅读解决方案,但似乎这个错误取决于代码,不幸的是我似乎无法弄清楚如何修复它。

任何想法如何解决错误或导致错误的原因?

这是完整的错误:

  

注意:未定义的偏移量:在600行的C:\ wamp \ www \ kids_house \ php \ functions.php中为1   注意:未定义的偏移量:第601行的C:\ wamp \ www \ kids_house \ php \ functions.php中的2

1 个答案:

答案 0 :(得分:2)

这两行抛出错误的原因是因为它的值不是yyyy / mm / dd,正如您所期望的那样:

$explodedDate=explode('/',$row['enrollmentdate']);

如果你看一下像这样抛出错误的值,你应该看到问题:

$thisMonth=$_POST['month']; //the value is today's date's month
$thisYear=$_POST['year']; //the value is today's date's year
$thisDay=$_POST['day']; //the value is today's date's day

$table=mysql_query("SELECT * FROM `kids` WHERE `debt`!='0'") or die(mysql_error());
$debt=0;

while($row=mysql_fetch_assoc($table)){
    $explodedDate=explode('/',$row['enrollmentdate']);

    if ( count( $explodedDate ) <= 1 ) {
        var_dump( $row );   //this will show you the row that is causing the notice
        var_dump( $explodedDate );   //this will show you the date
        die();
    }

    $theYear=$explodedDate[0];
    $theMonth=$explodedDate[1]; //this line throws the error
    $theDay=$explodedDate[2]; //and also this line
    if((int)$theYear==(int)$thisYear && (int)$theMonth==(int)$thisMonth){
        if((int)$theDay==(int)$thisDay || (int)$thisDay==0){
            $debt+=$row['debt'];
            }
        }
    }

如果您想使用逗号重试格式为yyyy,mm,dd的注册日期,则可以执行此操作。这不是最优雅的解决方案,但听起来你可能不得不这么脏。

$thisMonth=$_POST['month']; //the value is today's date's month
$thisYear=$_POST['year']; //the value is today's date's year
$thisDay=$_POST['day']; //the value is today's date's day

$table=mysql_query("SELECT * FROM `kids` WHERE `debt`!='0'") or die(mysql_error());
$debt=0;

while($row=mysql_fetch_assoc($table)){
    $explodedDate=explode('/',$row['enrollmentdate']);

    //try again with commas
    if ( count( $explodedDate ) == 0 ) {
        $explodedDate=explode(',',$row['enrollmentdate']);         
    }

    //skip the record if still no enrollment date
    if ( count( $explodedDate ) == 3 ) {

        $theYear=$explodedDate[0];
        $theMonth=$explodedDate[1]; //this line throws the error
        $theDay=$explodedDate[2]; //and also this line
        if((int)$theYear==(int)$thisYear && (int)$theMonth==(int)$thisMonth){
            if((int)$theDay==(int)$thisDay || (int)$thisDay==0){
                $debt+=$row['debt'];
                }
            }
        }
    }