12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- require_once 'conn.php';
- checkLogin();
- if (!checkIfAdmin()) {
- echo "<script>alert('您没有权限删除订单,只有管理员才能执行此操作'); window.location.href='order.php';</script>";
- exit;
- }
- $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
- $keys = urlencode($_GET['keys'] ?? '');
- $page = $_GET['page'] ?? 1;
- if ($id <= 0) {
- echo "<script>alert('无效的订单ID');location.href='order.php?keys=$keys&Page=$page';</script>";
- exit;
- }
- $checkSql = "SELECT id FROM orders WHERE id = $id";
- $checkResult = mysqli_query($conn, $checkSql);
- if (mysqli_num_rows($checkResult) === 0) {
- echo "<script>alert('订单不存在');location.href='order.php?keys=$keys&Page=$page';</script>";
- exit;
- }
- mysqli_autocommit($conn, FALSE);
- $error = false;
- try {
-
- $deleteItemsSql = "DELETE FROM order_items WHERE order_id = $id";
- if (!mysqli_query($conn, $deleteItemsSql)) {
- throw new Exception("删除订单项目失败: " . mysqli_error($conn));
- }
-
-
- $deleteOrderSql = "DELETE FROM orders WHERE id = $id";
- if (!mysqli_query($conn, $deleteOrderSql)) {
- throw new Exception("删除订单失败: " . mysqli_error($conn));
- }
-
-
- mysqli_commit($conn);
- echo "<script>alert('订单删除成功');location.href='order.php?keys=$keys&Page=$page';</script>";
- } catch (Exception $e) {
-
- mysqli_rollback($conn);
- echo "<script>alert('删除订单时发生错误: " . $e->getMessage() . "');location.href='order.php?keys=$keys&Page=$page';</script>";
- }
- mysqli_autocommit($conn, TRUE);
- exit;
- ?>
|