0

我正在尝试将电子邮件发送到数据库中包含的多个电子邮件地址并分类到记录集中...记录集有多个列,但我只需要一个:“电子邮件”。我知道如果我将它们放在一个数组中,我可以将它们内爆并用逗号分隔它们,但我不确定如何使用记录集列来做到这一点。有谁知道怎么做?

这是记录集代码:

$colname_rsAllLeads = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_rsAllLeads = $_SESSION['MM_Username'];
}
mysql_select_db($database_myBackOfficeConn, $myBackOfficeConn);
$query_rsAllLeads = sprintf("SELECT Email FROM Leads WHERE `User` = %s ORDER BY FullName ASC", GetSQLValueString($colname_rsAllLeads, "text"));
$rsAllLeads = mysql_query($query_rsAllLeads, $myBackOfficeConn) or die(mysql_error());
$row_rsAllLeads = mysql_fetch_assoc($rsAllLeads);
$totalRows_rsAllLeads = mysql_num_rows($rsAllLeads);

新代码:

$colname_rsAllLeads = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_rsAllLeads = $_SESSION['MM_Username'];
}
mysql_select_db($database_myBackOfficeConn, $myBackOfficeConn);
$query_rsAllLeads = sprintf("SELECT Email FROM Leads WHERE `User` = %s ORDER BY FullName ASC", GetSQLValueString($colname_rsAllLeads, "text"));
$rsAllLeads = mysql_query($query_rsAllLeads, $myBackOfficeConn) or die(mysql_error());
$row_rsAllLeads = mysql_fetch_assoc($rsAllLeads);
$totalRows_rsAllLeads = mysql_num_rows($rsAllLeads);

$row_rsAllLeads = array();
while( $row = mysql_fetch_assoc($rsAllLeads) ) {
   $row_rsAllLeads[] = $row;
}

$glued = "";
$separator = "";
foreach( $row_rsAllLeads as $row ) {
   $glued .= $separator . $row['Email'];
   $separator = ", ";
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
} 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {

    $startcode = $_POST['messagefield'];
    $replaced = preg_replace( '/\\\\(?="|\')/', '', $startcode );
    echo $replaced;
    $collectedleads = $row_rsAllLeads['Email'];
    echo $collectedleads;

    /*
 $to = $collectedleads;
 $subject = $_POST['subjectfield'];
 $body = $replaced;
 $headers = "MIME-Version: 1.0\r\n"; 
 $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
 $headers .= "From: " . $row_rs_CurrentUser['FirstName'] . " " . $row_rs_CurrentUser['LastName'] . " <" . $row_rs_CurrentUser['Email'] . ">";
 if (mail($to, $subject, $body, $headers)) {
  } else {
   echo("<p>Message delivery failed...</p>");
  }
  */

  $insertSQL = sprintf("INSERT INTO PendingEmails (`to`, subject, message) VALUES (%s, %s, %s)",
                       GetSQLValueString($row_rsAllLeads['Email'], "text"),
                       GetSQLValueString($_POST['subjectfield'], "text"),
                       GetSQLValueString($_POST['messagefield'], "text"));

  mysql_select_db($database_myBackOfficeConn, $myBackOfficeConn);
  $Result1 = mysql_query($insertSQL, $myBackOfficeConn) or die(mysql_error());

  $insertGoTo = "Email Sent.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
4

1 回答 1

1

你需要自己粘上它。implode()在这里没有用,因为你必须遍历你的记录集,提取Email,放入单独的数组,然后implode(). 但是由于您已经在迭代,您可以将它粘合而不使用临时数组和implode(),如下所示:

$glued = "";
$separator = "";
foreach( $row_rsAllLEads as $row ) {
   $glued .= $separator . $row['Email'];
   $separator = ", ";
}

编辑#1

您的代码也需要修复,因为您只获取第一个行集,而不是全部匹配您的 where 子句。它应该是这样的(你也应该将变量名从 更改row_...rows_...):

$row_rsAllLeads = array();
while( $row = mysql_fetch_assoc($rsAllLeads) ) {
   $row_rsAllLeads[] = $row;
}

所以毕竟$row_rsAllLeads应该是数组数组。

于 2012-10-28T14:13:58.067 回答