I can’t approve or cancel the order if there’s only one order inserted in my database

When there is only one order present in my database, the “Approve” and “Cancel” buttons do not function as expected. However, if there are multiple orders in the database, these buttons work properly

java script

function fetchOrders(status) {
    const xhr = new XMLHttpRequest();
    xhr.open('POST', 'fetch_pending_orders.php', true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.onload = function() {
      if (xhr.status === 200) {
        // Insert fetched orders as cards into the orderCards container
        document.getElementById('orderCards').innerHTML = xhr.responseText;
      }
    };
    xhr.send('product_status=' + status);
  }
back end

$status = $_POST['product_status'] ?? 'Pending';

if ($status == 'all') {
    $sql = "SELECT *,
               IF(product_status IS NULL OR product_status = '', 'Pending', product_status) AS computed_status
        FROM tbl_orders
        WHERE payment_method IS NOT NULL AND payment_method != ''";
    $stmt = $db->prepare($sql);
} elseif ($status == 'To Pay') {
    $sql = "SELECT *, 
                   IF(product_status IS NULL OR product_status = '', 'Pending', product_status) AS computed_status
            FROM tbl_orders 
            WHERE (product_status IS NULL OR product_status = '') AND payment_method IS NOT NULL AND payment_method != ''";
    $stmt = $db->prepare($sql);
} elseif ($status == 'To Deliver') {
    $sql = "SELECT *, 
                   IF(product_status IS NULL OR product_status = '', 'Pending', product_status) AS computed_status
            FROM tbl_orders 
            WHERE product_status = 'Approved' AND order_type = 'To Deliver' AND payment_method IS NOT NULL AND payment_method != ''";
    $stmt = $db->prepare($sql);
}  elseif ($status == 'To Pickup') {
    $sql = "SELECT *, 
                   IF(product_status IS NULL OR product_status = '', 'Pending', product_status) AS computed_status
            FROM tbl_orders 
            WHERE product_status = 'Approved' AND order_type = 'To Pickup' AND payment_method IS NOT NULL AND payment_method != ''";
    $stmt = $db->prepare($sql);
} elseif ($status == 'Completed') {
    $sql = "SELECT *, 
                   IF(product_status IS NULL OR product_status = '', 'Pending', product_status) AS computed_status
            FROM tbl_orders 
            WHERE product_status = 'Completed' AND payment_method IS NOT NULL AND payment_method != ''";
    $stmt = $db->prepare($sql);
} elseif ($status == 'Cancelled') {
    $sql = "SELECT *, 
                   IF(product_status IS NULL OR product_status = '', 'Pending', product_status) AS computed_status
            FROM tbl_orders 
            WHERE product_status = 'Cancelled' AND payment_method IS NOT NULL AND payment_method != ''";
    $stmt = $db->prepare($sql);
} else {
    $sql = "SELECT *, 
                   IF(product_status IS NULL OR product_status = '', 'Pending', product_status) AS computed_status
            FROM tbl_orders 
            WHERE order_type = ? AND payment_method IS NOT NULL AND payment_method != ''";
    $stmt = $db->prepare($sql);
    $stmt->bind_param('s', $status);
}

// Execute the statement and fetch results
$stmt->execute();
$result = $stmt->get_result();

$output = '';
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
    $order_id = $row['order_id'];
    
    $output .= '<div class="order-card" style="display: flex; align-items: center; justify-content: space-between; padding: 20px; border: 1px solid #ddd; margin-bottom: 10px;">';

    // Product Image
    $output .= '<div class="product-image" style="flex: 1;">';
    $output .= '<img src="../variety_pictures/' . htmlspecialchars($row['variety_image']) . '" alt="Product Image" style="width: 100%;">';
    $output .= '</div>';

    // Product Details
    $output .= '<div class="order-info" style="flex: 2; padding-left: 20px;">';
    $output .= '<h2 style="margin: 0 0 10px 0;">' . htmlspecialchars($row['variety_name']) . '</h2>';
    $output .= '<p>Quantity: ' . htmlspecialchars($row['quantity']) . '</p>';
    $output .= '<p>Order Total: ₱ ' . number_format($row['total_price'], 2) . '</p>';
    $output .= '<p>Product Status: <strong>' . 
    (!empty($row['computed_status']) ? htmlspecialchars($row['computed_status']) : 'Pending') . 
    '</strong></p>';

    $output .= '<p>Payment Method: ' . htmlspecialchars($row['payment_method']) . '</p>';
    $output .= '<p>Order Option: ' . htmlspecialchars($row['order_type']) . '</p>';
    $output .= '</div>';

    // Receiver and Actions
    $output .= '<div class="order-actions" style="flex: 1; text-align: left;">';
    $output .= '<p>Order ID: ' . htmlspecialchars($row['order_id']) . '</p>';
    $output .= '<p>Receiver Name: ' . $receiver_name . '</p>';
    $output .= '<p>Contact: ' . htmlspecialchars($row['phoneNumber']) . '</p>';
    $output .= '<p>Address: ' . htmlspecialchars($row['address']) . '</p>';
    
    // Show "View Payment" button only if payment_method is "Online Payment"
    if ($row['payment_method'] === 'Online Payment') {
      $output .= '<button type="button" class="btn btn-secondary w-100 mb-3" onclick="viewPayment(' . $row['order_id'] . ')">View Payment</button>';
    }

    if ($row['product_status'] == 'Completed') {
      // Display only the "Completed" status without buttons
      $output .= '<p><strong>Order Status:</strong> Completed</p>';
    } elseif ($row['product_status'] == 'Cancelled') {
      // Display only the "Cancelled" status without buttons
      $output .= '<p><strong>Order Status:</strong> Cancelled</p>';
    } else {
      // Approve and Deny Buttons for other statuses
      $output .= '<form method="POST" action="update_order_status.php">';
      $output .= '<input type="hidden" name="order_id" value="' . $row['order_id'] . '">';
      $output .= '<button type="submit" class="btn btn-success w-100 mb-3 ml" name="product_status" value="Approved">Approve</button><br>';
      $output .= '<button type="submit" class="btn btn-danger w-100 ml" name="product_status" value="Cancelled">Deny</button>';
        $output .= '</form>';
    }

    $output .= '</div>';
    $output .= '</div>';
}

echo $output;

} else {
    $output = '<p>No orders found for ' . htmlspecialchars($status) . '</p>';
}
<?php
require '../config.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $order_id = $_POST['order_id'] ?? null;
    $product_status = $_POST['product_status'] ?? null;

    if (in_array($product_status, ['Approved', 'Cancelled'], true)) {
        $sql = "UPDATE tbl_orders SET product_status = ? WHERE order_id = ?";
        $stmt = $db->prepare($sql);
        $stmt->bind_param('si', $product_status, $order_id);

        if ($stmt->execute()) {
            echo json_encode(['success' => true, 'message' => 'Order status updated successfully.']);
            header('location: pending.php');
            exit();
        } else {
            echo json_encode(['success' => false, 'message' => 'Failed to update order status.']);
            header('location: pending.php');
            exit();
        }
    } else {
        echo json_encode(['success' => false, 'message' => 'Invalid status value.']);
    }
}

?>

Even if there is only one order present in the database, I expect the “Approve” and “Cancel” buttons to function correctly, just as they do when multiple orders are present.

New contributor

Eyeyron is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật