我有一系列电子邮件,用于在MySQL中查找各自的CourseID配对。

循环应查找与该CourseID关联的每个email并将其附加到消息中,然后在完成时将其发送出去。但是,电子邮件会发出它找到的第一个$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";

关于php - 使用循环将查询变量附加到消息中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23208502/

10-10 23:40