Php 未定义的偏移量:字段为空时为1

Php 未定义的偏移量:字段为空时为1,php,mysql,Php,Mysql,我正在使用脚本发送邮件,但由于我的某些字段为空,因此出现了此错误 我的密码是 $studentid = explode(',',$row['studentid']); $Issue = explode(',',$row['Issue']); $Ret = explode(',',$row['ret']); $Consume = explode(',',$row['Consume']); $Lost = explode(',',$row['Lost']); $Damage = explod

我正在使用脚本发送邮件,但由于我的某些字段为空,因此出现了此错误

我的密码是

    $studentid = explode(',',$row['studentid']);
$Issue = explode(',',$row['Issue']);
$Ret = explode(',',$row['ret']);
$Consume = explode(',',$row['Consume']);
$Lost = explode(',',$row['Lost']);
$Damage = explode(',',$row['Damage']);
$Item = explode(',',$row['Item']);
$Description = explode(',',$row['Description']);
$Balance = explode(',',$row['Balance']);
$subject = "Inventory Detail";

// Create a line for each item/balance

    foreach($studentid as $key => $studentid){

$emailBody .= "<tr>";
    $emailBody .= '<td style="text-align:center;">'.$serial999++.'</td>';
    $emailBody .= '<td style="text-align:center;">'.$Item[$key].'</td>';
    $emailBody .= '<td style="text-align:center;">'.$Description[$key].'</td>';
    $emailBody .= '<td style="text-align:center;">'.$Issue[$key].'</td>';
    $emailBody .= '<td style="text-align:center;">'.$Ret[$key].'</td>';
    $emailBody .= '<td style="text-align:center;">'.$Consume[$key].'</td>';
    $emailBody .= '<td style="text-align:center;">'.$Lost[$key].'</td>';
    $emailBody .= '<td style="text-align:center;">'.$Damage[$key].'</td>';
    $emailBody .= '<td style="text-align:center;">'.$Balance[$key].'</td>';
    }
    $emailBody .= "</tr>";
如何解决这个问题

我的全部代码是这个

<?php
set_time_limit(120);
$con=mysqli_connect("localhost","root","","inventory");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result3 = mysqli_query($con,"SELECT   email, name, uid, GROUP_CONCAT(uid) as studentid, GROUP_CONCAT(item) as Item, GROUP_CONCAT(descr) as Description,  GROUP_CONCAT(issue) as Issue, GROUP_CONCAT(ret) as ret, GROUP_CONCAT(consume) as Consume, GROUP_CONCAT(lost) as Lost, GROUP_CONCAT(damage) as Damage, GROUP_CONCAT(Balance) as Balance FROM inventory_mail GROUP BY uid");
while($row = mysqli_fetch_array($result3))
{
if($row['email'])
{
$to = $row['email'];
$emailBody = "";
$emailBody .= "<b>Dear</b>  ".$row['name'].",<br/><br/>";

    $emailBody .= "<table border='0' width='338' height='57'>";
    $emailBody .="<tr>";
    $emailBody .= "<td width='100'><b>Enroll No</b></td>";
    $emailBody .= "<td>: ".$row['uid']."</td>";
    $emailBody .="</tr>";
    $emailBody .= "</table>";
$today = date("d-m-Y H:i:s");
$emailBody .= "<br/>Kindly find your Inventory details till <b>".$today."</b><br/><br/>";

$emailBody .="<table class=MsoTableGrid border=1 cellspacing=0 cellpadding=2 style=border-collapse: collapse; border: medium none id=table1 align=center  width=800>
<tr>
<th>S.no</th>
<th>Item</th>
<th>Description</th>
<th>Issue</th>
<th>Return</th>
<th>Consume</th>
<th>Lost</th>
<th>Damage</th>
<th>Balance</th>
</tr>";
$serial999 = "1";
// explode the Pin/balances on the comma's
$studentid = explode(',',$row['studentid']);
$Issue = explode(',',$row['Issue']);
$Ret = explode(',',$row['ret']);
$Consume = explode(',',$row['Consume']);
$Lost = explode(',',$row['Lost']);
$Damage = explode(',',$row['Damage']);
$Item = explode(',',$row['Item']);
$Description = explode(',',$row['Description']);
$Balance = explode(',',$row['Balance']);
$subject = "Inventory Detail";

// Create a line for each item/balance

    foreach($studentid as $key => $studentid){

$emailBody .= "<tr>";
    $emailBody .= '<td style="text-align:center;">'.$serial999++.'</td>';
    $emailBody .= '<td style="text-align:center;">'.$Item[$key].'</td>';
    $emailBody .= '<td style="text-align:center;">'.$Description[$key].'</td>';
    $emailBody .= '<td style="text-align:center;">'.$Issue[$key].'</td>';
    $emailBody .= '<td style="text-align:center;">'.$Ret[$key].'</td>';
    $emailBody .= '<td style="text-align:center;">'.$Consume[$key].'</td>';
    $emailBody .= '<td style="text-align:center;">'.$Lost[$key].'</td>';
    $emailBody .= '<td style="text-align:center;">'.$Damage[$key].'</td>';
    $emailBody .= '<td style="text-align:center;">'.$Balance[$key].'</td>';
    }
    $emailBody .= "</tr>";


$emailBody .= "</table>";

$emailBody .= "<br/>For More Details Login to http://lotus.edu.in/stafferp<br/><br/>Thanks,<br/>Lotus Institute of Management<br/>ERP System";

$emailBody= chunk_split(base64_encode($emailBody));
$headers = "From: Erp@lotus.edu.in\r\n";
$headers .= "Reply-To: erp@lotus.edu.in\r\n";
$headers .= "Return-Path: erp@lotus.edu.in\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n\r\n";

      if(mail($to, $subject, $emailBody, $headers)) {
          echo 'Success:';
          echo '-----';
          echo $row['uid'];
          echo '-----';
          echo $row['name'];
          echo '-----';
          echo 'Email sent successfully!<br/><br/>';
      } else {
      }

}
else {
          echo 'Failure:';
          echo '-----';
          echo $row['uid'];
          echo '-----';
          echo $row['name'];
          echo '-----';
          echo 'Failure: Email was not sent!<br/><br/>';
      }
}

mysqli_close($con);
?>
请尝试对变量执行此操作

     if (isset($var[index])) {
         "do something here"
     }
首先检查变量是否存在,然后只有您可以使用它。希望能有帮助

就像你的情况一样

 if (isset($row['studentid'])) {
       $studentid = explode(',',$row['studentid']);
  }
 else{
      $studentid = NULL;
  } 

看看你的代码,你现在有一个更大的问题,那就是
组CONCAT
,如果你有一个
null
empty
条目,它会给你一个错误的数量和结果顺序

例如,假设结果应该是:

studentid   item
1           1
2           2
3           null
4           4
但是,
null
将从结果中忽略,因此您将实际获得:

studentid   item
1           1
2           2
3           4
4
接下来,由于最后一个项目不存在,
$item
将不会有被读取的索引量,更不用说
组\u CONCAT
也可以加扰或有更少的条目,它将给您提供您在
foreach
上提到的错误以及显示的错误数据,其他变量也是如此

为了防止错误消息发生,您必须在使用变量之前验证该变量,如下所示:

// Create a line for each item/balance
foreach($studentid as $key => $studentid)
{
    //... rest of your code
    $emailBody .= '<td style="text-align:center;">' . (!isset($Item[$key]) ? '' : $Item[$key]) .'</td>';
    //... rest of your code
}
    SELECT email, name, uid, 
           GROUP_CONCAT(IFNULL(uid, '')) as studentid, 
           GROUP_CONCAT(IFNULL(item, '')) as Item, 
           GROUP_CONCAT(IFNULL(descr, '')) as Description,  
           GROUP_CONCAT(IFNULL(issue, '')) as Issue, 
           GROUP_CONCAT(IFNULL(ret, '')) as ret, 
           GROUP_CONCAT(IFNULL(consume, '')) as Consume, 
           GROUP_CONCAT(IFNULL(lost, '')) as Lost, 
           GROUP_CONCAT(IFNULL(damage, '')) as Damage, 
           GROUP_CONCAT(IFNULL(Balance, '')) as Balance 
      FROM inventory_mail 
  GROUP BY uid

通过使用
IFNULL(项“”)
如果给定行为
null
,它会将其设置为空,并作为空行出现在数组中。

inventory.php第62行的
explode
是???看起来您的一些
explode
在数组中返回的元素不超过1个?你的一些学生可能没有与他们相关的特定信息?无论哪种方式,您在其中一个数组中缺少某些偏移量。指定了哪一行。这些错误表示
$Ret=explode(',',$row['Ret'])$消耗=分解(',',$row['Consume'])$Lost=爆炸(“,”,$row['Lost'])$伤害=爆炸(“,”,$row[“伤害])
这些代码数组是否返回null??这些字段在mysql中没有数据它们为null,你能给我看一下我修改过的代码吗?相同的问题没有成功如果数组的索引不可用,你就不能使用数组值,所以你必须在使用索引时应用检查来读取我的答案的顶部,你就会知道为什么它错了,这意味着不可能,现在我该怎么做才能完成呢this@user3210029请参阅关于我的问题的更新,关于如何更改MySQL查询,这可能会修复它。如果您100%确定其中一列不会为null,则可以删除该检查。
    SELECT email, name, uid, 
           GROUP_CONCAT(IFNULL(uid, '')) as studentid, 
           GROUP_CONCAT(IFNULL(item, '')) as Item, 
           GROUP_CONCAT(IFNULL(descr, '')) as Description,  
           GROUP_CONCAT(IFNULL(issue, '')) as Issue, 
           GROUP_CONCAT(IFNULL(ret, '')) as ret, 
           GROUP_CONCAT(IFNULL(consume, '')) as Consume, 
           GROUP_CONCAT(IFNULL(lost, '')) as Lost, 
           GROUP_CONCAT(IFNULL(damage, '')) as Damage, 
           GROUP_CONCAT(IFNULL(Balance, '')) as Balance 
      FROM inventory_mail 
  GROUP BY uid