使用循环将查询变量追加到消息中


Using a loop to append query variables to a message

我有一组电子邮件,用来在MySQL中查找它们各自的CourseID

循环应该找到与该email相关联的每个CourseID,并将其附加到消息上,然后在完成后将其发送出去。然而,电子邮件会发送它找到的第一个$row['CourseID'],而不会显示邮件的其余部分或找到的任何其他$row['CourseID']

这是我的密码,有人能告诉我我缺了什么吗?

 <?php
  $courseList=array();
  $studentList=array();
  $subject="Course Surveys";
  $con=mysqli_connect("","","","");
  // Check connection
  if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
  //populate the course list array
  foreach ($_POST['queue'] as $cID)
    { 
    array_push($courseList, $cID);
    }
  //find all the student emails in each course and put them into an array
  for ($i=0; $i<=count($courseList);$i++)
    {
      $theCourse=$courseList[$i];
      $foundemails=mysqli_query($con,"SELECT StudentEmail FROM Students WHERE CourseID='$theCourse'");
      while($row=mysqli_fetch_array($foundemails)){
      array_push($studentList, $row['StudentEmail']);
      }
    }
  //strip email array of duplicates & restructure array
  $studentList = array_unique($studentList);
  $studentList = array_values($studentList);
  //Send emails
   for ($i=0;$i<=count($studentList);$i++)
   {
    $message = "Hello ".$studentList[$i].", please take the time to complete the following surveys:'n";
    $result=mysqli_query($con,"SELECT * FROM Students WHERE StudentEmail='$studentList[$i]'"); /* USE ORDER BY StudentEmail */
    while($row=mysqli_fetch_array($result)){
    $hashed=md5($studentList[$i].$row['CourseID']);    
    $message .= $row['CourseID'].":'n http://khaledkloub.com/projects/admin/survey.php?id=".$hashed."'n";
    }
    mail($studentList[$i], $subject, $message, "From: Course Survey Manager'n");
   }
 mysqli_close($con);
?>
$message .= $row['CourseID']."'n";

$message = $message . $row['CourseID']."'n";