我想做四件事,但如果我注释掉第 3 步,我只能让第 1、2 和 4 步工作:(1) 从订单标题中获取 first_name 以包含在电子邮件通知中,(2) 删除订单 header ,(3) 删除订单项目,以及 (4) 删除订单 header 时发送电子邮件通知。
以下代码仅在步骤 3 被注释掉时执行步骤 1、2 和 4:
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// (1) Get first_name
$sql = "SELECT id, first_name FROM Orders where id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$first_name = $data['first_name'];
$last_name = $data['last_name'];
// (2) delete Header record
$sql = "DELETE FROM Orders WHERE id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($id));
// (3) Delete Order Items
// $sql = "DELETE FROM Order_Items WHERE id = ?";
// $q = $pdo->prepare($sql);
// $q->execute(array($id));
// I moved these 2 lines to the bottom as suggested by Martin
Database::disconnect();
header("Location: index.php");
// (4) send email notification
$to = "email address 1";
$email_from = "email address 2";
$today = date("m.d.y");
$body = "";
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers = "MIME-Version: 1.0\n" .
"From: {$email_from}\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$subject = "Order record deleted - "
. $id . " "
. $first_name . " "
. $last_name . "-"
. $today;
mail($to, $subject, $body, $headers);
当我取消注释步骤 3 时,不仅步骤 3 不起作用,而且步骤 4 也无法起作用。
错误日志可能有帮助,但我想不出解决办法。它说:
Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id'
在 $q->execute(array($id)) 出现的行抛出错误。我唯一能想到的是第 1 步和第 2 步在同一张表(订单)上工作,所以它不介意。但现在我在新表 (Order_Items) 上引入了一个命令,它变得困惑了。
最佳答案
您的 Order_Items
表是否有名为 id
的列?
@Martin - you're a genius. No. It is called orders_id. Thank you!
不,唉I am no genius ,我只是简单地阅读了错误信息:
Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id'
总是阅读你的错误信息!!
关于php - 多个 MySQL DELETE 禁用电子邮件通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43350441/