簡體   English   中英

向所有用戶發送自定義電子郵件-PHP代碼問題

[英]Sending Custom Email to All Users - PHP Code Issue

我目前正在制作一個概念原型,因此正在使用PHPMailer生成/發送電子郵件。 我很快就會搬到類似SendGrid的地方,但這不是當前的問題。 我需要向數據庫中的每個用戶發送電子郵件,但是將根據數據庫中存儲的元素(特定於單個用戶)自定義電子郵件。

我讓PHPMailer從數據庫中提取字段並發送郵件。 但是,當我嘗試增加在所有用戶之間循環並向每個用戶生成電子郵件的功能時,我似乎遇到了一些PHP / SQL問題。

“用戶”表具有以下字段:cid,電子郵件,fname,lname。 我需要拉cid才能拉出數據庫中的其他信息,並且需要電子郵件,以便我知道將消息發送到哪里。 這是我的SQL / PHP:

require_once('/PHPMailer_v51/class.phpmailer.php');
include $_SERVER['DOCUMENT_ROOT'] . '/NG/includes/db.inc.php';

try
{
$sql = 'SELECT * FROM users';
$result = $pdo->prepare($sql);
$result->execute();
}

catch (PDOException $e)
{
$error = 'Error fetching user information.';
include 'error.html.php';
exit();
}

 foreach ($result as $row);
 {
  try
  {
    $high_result = $pdo->query('SELECT title FROM book WHERE topic = "high" and cid = $row['cid']');
  }
  catch (PDOException $e)
  {
  $error = 'Error fetching fields from database!';
  include 'error.html.php';
  exit();
  }
  .... the rest of the for statement....

$ high_result行上出現意外T_STRING錯誤。 “用戶”數據庫具有以下字段:cid,電子郵件,fname,lname。 我無法在sql語句中引用數組嗎? 如果沒有,關於我應該如何做的任何想法?

謝謝,

纏繞

你有一個 ”;” 在你的foreach行,刪除它,它應該可以工作

查詢應該是

$high_result = $pdo->query('SELECT title FROM book WHERE topic = "high" and cid = $row[cid]');

您在$row['cid']包含了' ,這就是錯誤。 ; 在foreach循環中也將其刪除錯誤。

foreach ($result as $row) // The semicoln here is not required
 //... The rest of the statements

$high_result = $pdo->query("SELECT title FROM book WHERE topic = "high" and cid = '. $pdo->quote($row[cid]));

如果您的字段是字符串,則使用引號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM