40th attempl at fixing cart.php

This commit is contained in:
Frank Harris 2025-11-09 15:46:32 -05:00
parent e90d2ec015
commit 85cd8e1605

View file

@ -22,14 +22,20 @@ if ($user_id <= 0) {
} }
// Connect to database using mysqli // Connect to database using mysqli
$db = mysqli_connect($db_host, $db_user, $db_pass, $db_name); $db = @mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$db) { $db_error = '';
die('Database connection failed: ' . mysqli_connect_error());
}
// Fetch all unpaid invoices for this user // Initialize defaults
$invoices = []; $invoices = [];
$total_amount = 0.00; $total_amount = 0.00;
$cart_empty = true;
if (!$db) {
// Do not fatal: show a friendly message in the UI instead. This allows the page to load
// even when DB is temporarily unreachable (useful for local development).
$db_error = 'Database connection failed: ' . mysqli_connect_error();
} else {
// Fetch all unpaid invoices for this user
$query = "SELECT i.*, s.game_key, s.game_name $query = "SELECT i.*, s.game_key, s.game_name
FROM {$table_prefix}billing_invoices i FROM {$table_prefix}billing_invoices i
LEFT JOIN {$table_prefix}billing_services s ON i.service_id = s.service_id LEFT JOIN {$table_prefix}billing_services s ON i.service_id = s.service_id
@ -46,6 +52,7 @@ if ($result) {
// If cart is empty, show message // If cart is empty, show message
$cart_empty = count($invoices) === 0; $cart_empty = count($invoices) === 0;
}
// Coupon handling // Coupon handling
$coupon_code = ''; $coupon_code = '';
@ -65,6 +72,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['apply_coupon'])) {
if (empty($submitted_code)) { if (empty($submitted_code)) {
$coupon_error = 'Please enter a coupon code.'; $coupon_error = 'Please enter a coupon code.';
} else {
if (!$db) {
$coupon_error = 'Coupon system unavailable: no database connection.';
} else { } else {
// Validate coupon // Validate coupon
$safe_code = mysqli_real_escape_string($db, $submitted_code); $safe_code = mysqli_real_escape_string($db, $submitted_code);
@ -133,6 +143,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['apply_coupon'])) {
} }
} }
} }
}
// Handle coupon removal // Handle coupon removal
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['remove_coupon'])) { if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['remove_coupon'])) {
@ -143,9 +154,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['remove_coupon'])) {
$applied_coupon = null; $applied_coupon = null;
} }
// Calculate discount if coupon is applied
// Calculate discount if coupon is applied // Calculate discount if coupon is applied
$discount_amount = 0; $discount_amount = 0;
if (!empty($coupon_code) && $coupon_discount_percent > 0) { if (!empty($coupon_code) && $coupon_discount_percent > 0 && $db) {
// Re-validate the coupon from session // Re-validate the coupon from session
$safe_code = mysqli_real_escape_string($db, $coupon_code); $safe_code = mysqli_real_escape_string($db, $coupon_code);
$coupon_query = "SELECT * FROM {$table_prefix}billing_coupons $coupon_query = "SELECT * FROM {$table_prefix}billing_coupons
@ -157,6 +169,9 @@ if (!empty($coupon_code) && $coupon_discount_percent > 0) {
$coupon_discount_percent = floatval($applied_coupon['discount_percent']); $coupon_discount_percent = floatval($applied_coupon['discount_percent']);
$discount_amount = $total_amount * ($coupon_discount_percent / 100); $discount_amount = $total_amount * ($coupon_discount_percent / 100);
} }
} else {
// No DB or no coupon: ensure discount is zero
$discount_amount = 0;
} }
$final_amount = $total_amount - $discount_amount; $final_amount = $total_amount - $discount_amount;
@ -185,7 +200,7 @@ $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https:
$host = $_SERVER['HTTP_HOST'] ?? 'localhost'; $host = $_SERVER['HTTP_HOST'] ?? 'localhost';
$siteBase = $protocol . $host; $siteBase = $protocol . $host;
mysqli_close($db); if ($db) mysqli_close($db);
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
@ -342,6 +357,12 @@ mysqli_close($db);
<body> <body>
<?php include(__DIR__ . '/includes/menu.php'); ?> <?php include(__DIR__ . '/includes/menu.php'); ?>
<div class="container"> <div class="container">
<?php if (!empty($db_error)): ?>
<div class="alert-error" style="margin-bottom:15px;">
<strong>Database error:</strong> <?php echo htmlspecialchars($db_error); ?>
<div style="font-size:0.9em;color:#333;margin-top:6px;">The cart is read-only while the database is unavailable.</div>
</div>
<?php endif; ?>
<h1>🛒 Shopping Cart</h1> <h1>🛒 Shopping Cart</h1>
<?php if ($cart_empty): ?> <?php if ($cart_empty): ?>