diff --git a/delete_process.php b/delete_process.php index 12c3a64..db134a4 100644 --- a/delete_process.php +++ b/delete_process.php @@ -6,16 +6,27 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['id'])) { try { $pdo = db(); - $stmt = $pdo->prepare("DELETE FROM processes WHERE id = :id"); - $stmt->bindParam(':id', $id, PDO::PARAM_INT); - if ($stmt->execute()) { + $pdo->beginTransaction(); + + // Delete associated steps first + $stmtSteps = $pdo->prepare("DELETE FROM process_steps WHERE process_id = :id"); + $stmtSteps->bindParam(':id', $id, PDO::PARAM_INT); + $stmtSteps->execute(); + + // Then delete the process + $stmtProcess = $pdo->prepare("DELETE FROM processes WHERE id = :id"); + $stmtProcess->bindParam(':id', $id, PDO::PARAM_INT); + if ($stmtProcess->execute()) { + $pdo->commit(); header('Location: index.php?success=processdeleted'); exit(); } else { + $pdo->rollBack(); header('Location: index.php?error=deletionfailed'); exit(); } } catch (PDOException $e) { + $pdo->rollBack(); error_log("DB Error: " . $e->getMessage()); header('Location: index.php?error=dberror'); exit();