注意:未定义的偏移量 php
Notice: Undefined Offset php
我有这个 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'];
}
}
}
我一直在整个互联网上阅读解决方案,但似乎这个错误取决于代码,不幸的是我似乎无法弄清楚如何修复它。
关于如何修复错误或错误原因的任何想法?
这是完整的错误:
Notice: Undefined offset: 1 in C:\wamp\www\kids_house\php\functions.php on line 600
Notice: Undefined offset: 2 in C:\wamp\www\kids_house\php\functions.php on line 601
这两行抛出错误的原因是因为这里的值不是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'];
}
}
}
}
我有这个 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'];
}
}
}
我一直在整个互联网上阅读解决方案,但似乎这个错误取决于代码,不幸的是我似乎无法弄清楚如何修复它。
关于如何修复错误或错误原因的任何想法?
这是完整的错误:
Notice: Undefined offset: 1 in C:\wamp\www\kids_house\php\functions.php on line 600 Notice: Undefined offset: 2 in C:\wamp\www\kids_house\php\functions.php on line 601
这两行抛出错误的原因是因为这里的值不是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'];
}
}
}
}