prepare( "SELECT a.*, u.nickname AS seller_nickname, u.rating AS seller_rating FROM ads a JOIN users u ON a.user_id = u.id WHERE a.id = :ad_id AND a.status = 'ACTIVE'" ); $stmt->bindParam(':ad_id', $ad_id, PDO::PARAM_INT); $stmt->execute(); $ad = $stmt->fetch(PDO::FETCH_ASSOC); if (!$ad) { $error = "Ad not found or is no longer active."; } } catch (PDOException $e) { $error = "Database error: " . $e->getMessage(); } // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && $ad) { $amount_crypto = $_POST['amount_crypto'] ?? 0; $amount_fiat = 0; // Basic validation if (!is_numeric($amount_crypto) || $amount_crypto <= 0) { $error = "Please enter a valid amount."; } else { $amount_fiat = $amount_crypto * $ad['fixed_price']; if ($amount_fiat < $ad['min_amount'] || $amount_fiat > $ad['max_amount']) { $error = "The amount is not within the ad's limits."; } elseif ($amount_crypto > $ad['available_amount']) { $error = "The requested amount exceeds the available amount in the ad."; } else { // All good, create the order try { $buyer_id = $current_user_id; $seller_id = $ad['user_id']; $status = 'AWAITING_PAYMENT'; // Or PENDING_CONFIRMATION depending on flow $pdo->beginTransaction(); // Create the order $order_stmt = $pdo->prepare( "INSERT INTO orders (ad_id, buyer_id, seller_id, amount_crypto, amount_fiat, status) VALUES (:ad_id, :buyer_id, :seller_id, :amount_crypto, :amount_fiat, :status)" ); $order_stmt->execute([ ':ad_id' => $ad_id, ':buyer_id' => $buyer_id, ':seller_id' => $seller_id, ':amount_crypto' => $amount_crypto, ':amount_fiat' => $amount_fiat, ':status' => $status ]); // Reduce available amount in the ad $ad_update_stmt = $pdo->prepare("UPDATE ads SET available_amount = available_amount - :amount_crypto WHERE id = :ad_id"); $ad_update_stmt->execute([':amount_crypto' => $amount_crypto, ':ad_id' => $ad_id]); $pdo->commit(); // Redirect to deals page header("Location: my_deals.php"); exit; } catch (PDOException $e) { $pdo->rollBack(); $error = "Failed to create the deal. Error: " . $e->getMessage(); } } } } ?>
Price: per
Limits: -
Available:
Payment Method: