order_delete.php 1.8 KB

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