order_delete.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. require_once 'conn.php';
  3. checkLogin();
  4. // 获取订单ID
  5. $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
  6. $keys = urlencode($_GET['keys'] ?? '');
  7. $page = $_GET['page'] ?? 1;
  8. // 验证参数
  9. if ($id <= 0) {
  10. echo "<script>alert('无效的订单ID');location.href='order.php?keys=$keys&Page=$page';</script>";
  11. exit;
  12. }
  13. // 验证订单所有权(只能删除自己的订单)
  14. $employee_id = $_SESSION['employee_id'];
  15. $checkSql = "SELECT id FROM orders WHERE id = $id AND employee_id = $employee_id";
  16. $checkResult = mysqli_query($conn, $checkSql);
  17. if (mysqli_num_rows($checkResult) === 0) {
  18. echo "<script>alert('订单不存在或您没有权限删除该订单');location.href='order.php?keys=$keys&Page=$page';</script>";
  19. exit;
  20. }
  21. // 开始事务处理
  22. mysqli_autocommit($conn, FALSE);
  23. $error = false;
  24. try {
  25. // 先删除订单项目
  26. $deleteItemsSql = "DELETE FROM order_items WHERE order_id = $id";
  27. if (!mysqli_query($conn, $deleteItemsSql)) {
  28. throw new Exception("删除订单项目失败: " . mysqli_error($conn));
  29. }
  30. // 删除订单主表
  31. $deleteOrderSql = "DELETE FROM orders WHERE id = $id AND employee_id = $employee_id";
  32. if (!mysqli_query($conn, $deleteOrderSql)) {
  33. throw new Exception("删除订单失败: " . mysqli_error($conn));
  34. }
  35. // 提交事务
  36. mysqli_commit($conn);
  37. echo "<script>alert('订单删除成功');location.href='order.php?keys=$keys&Page=$page';</script>";
  38. } catch (Exception $e) {
  39. // 回滚事务
  40. mysqli_rollback($conn);
  41. echo "<script>alert('删除订单时发生错误: " . $e->getMessage() . "');location.href='order.php?keys=$keys&Page=$page';</script>";
  42. }
  43. // 恢复自动提交
  44. mysqli_autocommit($conn, TRUE);
  45. exit;
  46. ?>