prepare("SELECT wallet_balance FROM users WHERE id = ?"); $stmt->execute([$user_id]); $wallet_balance = $stmt->fetchColumn(); if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['amount'])) { $amount = filter_input(INPUT_POST, 'amount', FILTER_VALIDATE_FLOAT); if ($amount === false || $amount <= 0) { $error = 'Please enter a valid withdrawal amount.'; } elseif ($amount > $wallet_balance) { $error = 'Withdrawal amount cannot exceed your wallet balance.'; } else { try { $db->beginTransaction(); // 1. Insert into withdrawals table $stmt = $db->prepare("INSERT INTO withdrawals (user_id, amount) VALUES (?, ?)"); $stmt->execute([$user_id, $amount]); $withdrawal_id = $db->lastInsertId(); // 2. Deduct from user's wallet $stmt = $db->prepare("UPDATE users SET wallet_balance = wallet_balance - ? WHERE id = ?"); $stmt->execute([$amount, $user_id]); // 3. Record the transaction $stmt = $db->prepare("INSERT INTO transactions (user_id, amount, type, description, related_withdrawal_id) VALUES (?, ?, 'withdrawal_request', 'Withdrawal request initiated', ?)"); $stmt->execute([$user_id, -$amount, $withdrawal_id]); $db->commit(); $message = 'Your withdrawal request has been submitted successfully. It will be processed shortly.'; // Refresh wallet balance $wallet_balance -= $amount; } catch (PDOException $e) { $db->rollBack(); $error = 'An error occurred. Please try again.'; // For debugging: error_log($e->getMessage()); } } } // Fetch recent withdrawals $stmt = $db->prepare("SELECT * FROM withdrawals WHERE user_id = ? ORDER BY created_at DESC LIMIT 10"); $stmt->execute([$user_id]); $withdrawals = $stmt->fetchAll(); ?>
₹
| Date | Amount | Status |
|---|---|---|
| No recent withdrawals. | ||
| ₹ | ||