我想做四件事,但如果我注释掉第3步,我只能进行第1,2和4步:(1)从订单头中获取first_name以包含在电子邮件通知中,(2)删除订单标题,(3)删除订单商品,以及(4)删除订单标题时发送电子邮件通知。
以下代码仅在步骤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步是在同一个表(Orders)上工作,所以它并不介意。但是现在我在一个新表(Order_Items)上引入了一个命令,它会让人感到困惑。
答案 0 :(得分:3)
您的Order_Items
表格是否有一个名为id
的列?
@Martin - 你是个天才。不,它叫做orders_id。谢谢!
不,唉I am no genius,我只是阅读了错误消息:
未捕获的异常' PDOException'消息' SQLSTATE [42S22]:未找到列:1054未知列' id'
始终阅读您的错误消息!!