Я не могу утвердить или отменить заказ, если в мою базу данных добавлен только один заказ.Php

Кемеровские программисты php общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Я не могу утвердить или отменить заказ, если в мою базу данных добавлен только один заказ.

Сообщение Anonymous »

Когда в моей базе данных присутствует только один заказ, кнопки «Одобрить» и «Отменить» не работают должным образом. Однако если в базе данных несколько заказов, эти кнопки работают корректно

Код: Выделить всё

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 .= '';

// Product Image
$output .= '';
$output .= '
[img]../variety_pictures/[/img]
';
$output .= '';

// Product Details
$output .= '';
$output .= '' . htmlspecialchars($row['variety_name']) . '';
$output .= '
Quantity: ' . htmlspecialchars($row['quantity']) . '
';
$output .= '
Order Total: ₱ ' . number_format($row['total_price'], 2) . '
';
$output .= '
Product Status: [b]' .
(!empty($row['computed_status']) ? htmlspecialchars($row['computed_status']) : 'Pending') .
'[/b]
';

$output .= '
Payment Method: ' . htmlspecialchars($row['payment_method']) . '
';
$output .= '
Order Option: ' . htmlspecialchars($row['order_type']) . '
';
$output .= '';

// Receiver and Actions
$output .= '';
$output .= '
Order ID: ' . htmlspecialchars($row['order_id']) . '
';
$output .= '
Receiver Name: ' . $receiver_name . '
';
$output .= '
Contact: ' . htmlspecialchars($row['phoneNumber']) . '
';
$output .= '
Address: ' . htmlspecialchars($row['address']) . '
';

// Show "View Payment" button only if payment_method is "Online Payment"
if ($row['payment_method'] === 'Online Payment') {
$output .= 'View Payment';
}

if ($row['product_status'] == 'Completed') {
// Display only the "Completed" status without buttons
$output .= '
[b]Order Status:[/b] Completed
';
} elseif ($row['product_status'] == 'Cancelled') {
// Display only the "Cancelled" status without buttons
$output .= '
[b]Order Status:[/b] Cancelled
';
} else {
// Approve and Deny Buttons for other statuses
$output .= '';
$output .= '';
$output .= 'Approve
';
$output .= 'Deny';
$output .= '';
}

$output .= '';
$output .= '';
}

echo $output;

} else {
$output = '
No orders found for ' . htmlspecialchars($status) . '
';
}
Даже если в базе данных присутствует только один заказ, я ожидаю, что кнопки «Одобрить» и «Отменить» будут работать правильно, так же, как и при наличии нескольких заказов.
Даже если в базе данных присутствует только один заказ, я ожидаю, что кнопки «Утвердить» и «Отменить» будут работать правильно, так же, как и при наличии нескольких заказов.
п>

Подробнее здесь: https://stackoverflow.com/questions/792 ... -in-my-dat
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Php»