【发布时间】: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,1和2将不会出现。你可以做一个hacky解决方案$theMonth=!empty($explodedDate[1]) ? $explodedDate[1] : '';。然后变量将是空的。您可能应该弄清楚为什么日期不存在..