【问题标题】:Notice: Undefined Offset php注意:未定义的偏移量 php
【发布时间】:2023-03-27 22:20:01
【问题描述】:

我有这个 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'];
            }
        }
    }

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

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

这是完整的错误:

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

【问题讨论】:

  • 如果你var_dump($explodedDate) 会返回什么?
  • @chris85 将完整错误添加到问题中
  • 好的,谢谢。那么$row['enrollmentdate']是什么?
  • 伙计们,请阅读以下 cmets:explode() 之后,它会在 array 中返回这些数字
  • 读错评论。是的,如果没有 /s,12 将不会出现。你可以做一个hacky解决方案$theMonth=!empty($explodedDate[1]) ? $explodedDate[1] : '';。然后变量将是空的。您可能应该弄清楚为什么日期不存在..

标签: php notice


【解决方案1】:

这两行抛出错误的原因是因为这里的值不是你期望的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'];
                }
            }
        }
    }

【讨论】:

  • 这是一个有趣的方法,但我修复了数据库字段 (enrollmentdate) 为空的错误,现在它可以按预期工作,非常感谢 +1
  • 我在发布解决方案后阅读了关于最初问题的最后一篇 cmets - 抱歉!很高兴你成功了。
  • 好吧,我不得不承认你的方法很棒,在尝试修复错误(失败并引发更多错误!)之后,我尝试了你跳过问题的方法,它就像一个魅力,谢谢又说了这么多;)
猜你喜欢
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 2016-10-01
  • 1970-01-01
  • 2015-08-01
  • 1970-01-01
相关资源
最近更新 更多