Bricked
Bricked

Reputation: 115

Multiple MySQL DELETE disabling email notification

I want to do four things, but I can only get step 1, 2 and 4 to work if I comment out step 3: (1) Get first_name from the order header to include in email notification, (2) Delete the order header, (3) Delete the Order items, and (4) Send email notification when the order header has been deleted.

The following code does step 1, 2 and 4 only if step 3 is commented out:

$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); 

When I un-comment step 3, not only does step 3 not work, but step 4 fails to work as well.

The error log may be of assistance, but I cannot figure out the solution. It says:

Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id'

The error is thrown on the line where the $q->execute(array($id)) appears. The only thing I can figure is that step 1 and step 2 are working on the same table (Orders) so it doesn't mind. But now that I introduce a command on a new table (Order_Items) it gets confused.

Upvotes: 0

Views: 71

Answers (1)

Martin
Martin

Reputation: 22760

Does your Order_Items table have a column called id?

@Martin - you're a genius. No. It is called orders_id. Thank you!

No, alas I am no genius, I simply read the error message:

Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id'

Always read your error messages!!

Upvotes: 3

Related Questions