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

时间:2014-03-18 07:02:37

标签: php mysql

我正在使用脚本发送邮件,但我收到此错误,因为我的某些字段为空

我的代码就是这个

    $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>";

错误就是这个

Notice: Undefined offset: 1 in C:\xampp\htdocs\mailtest\inventory.php on line 62

Notice: Undefined offset: 1 in C:\xampp\htdocs\mailtest\inventory.php on line 63

Notice: Undefined offset: 1 in C:\xampp\htdocs\mailtest\inventory.php on line 64

Notice: Undefined offset: 1 in C:\xampp\htdocs\mailtest\inventory.php on line 65

如何解决这个问题

我的完整代码就是这个

<?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);
?>

我收到了这个结果

S.no项目描述问题返回消耗损失平衡 1 A4_Rim。 19 1 19 2 Neck_Strip。 50 1 50 3 Pencil_cell。 1 0 4 Whiteboard_Marker。 2 1

而不是

S.no项目描述问题返回消耗损失平衡 1 A4_Rim。 19 19 2 Neck_Strip。 50 50 3 Pencil_cell。 1 1 0 4 Whiteboard_Marker。 2 1 1

它正在更改消耗列中值的顺序,它们应该是最后两行,但是它是在第一行中设置它们

2 个答案:

答案 0 :(得分:1)

查看您的代码,您现在有一个更大的问题,即GROUP_CONCAT,如果您有nullempty条目,它会给您错误的金额和订单结果。

例如,让我们说结果应该是:

studentid   item
1           1
2           2
3           null
4           4

但是结果中将省略null,因此您实际上会得到:

studentid   item
1           1
2           2
3           4
4

跟进,由于最后一项不存在,$Item将没有读取索引的数量,更不用说GROUP_CONCAT也可以加扰或减少条目,它会给你您在foreach上提到的错误以及显示的错误数据,与您拥有的其他变量相同。

Here is an DEMO of what your MySQL query does and why your results are not right.

为了阻止错误消息发生,您必须在使用之前验证变量:

// 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
}

基本上我们检查是否设置了$Item[$key],如果没有打印,则避免索引错误不会跟随当前索引。

要修复丢失的数据以使索引量正确,您必须更改MySQL查询,如下所示:

    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(item, ''),则使用null它会将其设置为空,并将作为空行出现在数组中。

答案 1 :(得分:0)

尝试使用变量

     if (isset($var[index])) {
         "do something here"
     }

首先检查变量是否存在,然后才能使用它。希望它有所帮助。

就像你的情况一样

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