query($sql_ngo); $ngo = $stmt_ngo->fetch(PDO::FETCH_ASSOC); if (!$ngo) { throw new Exception('No NGOs found in the database. Please register an NGO first.'); } $ngo_id = $ngo['id']; } catch (Exception $e) { // error_log($e->getMessage()); header('Location: ngo_dashboard.php?status=no_ngo'); exit(); } try { $pdo = db(); // Check if the listing is still available before claiming $checkSql = "SELECT status FROM food_listings WHERE id = :id"; $checkStmt = $pdo->prepare($checkSql); $checkStmt->bindParam(':id', $id, PDO::PARAM_INT); $checkStmt->execute(); $currentStatus = $checkStmt->fetchColumn(); if ($currentStatus === 'available') { $updateSql = "UPDATE food_listings SET status = 'claimed', ngo_id = :ngo_id WHERE id = :id"; $updateStmt = $pdo->prepare($updateSql); $updateStmt->bindParam(':ngo_id', $ngo_id, PDO::PARAM_INT); $updateStmt->bindParam(':id', $id, PDO::PARAM_INT); if ($updateStmt->execute()) { header('Location: ngo_dashboard.php?status=claimed'); } else { header('Location: ngo_dashboard.php?status=error'); } } else { // The listing was already claimed by someone else header('Location: ngo_dashboard.php?status=already_claimed'); } } catch (PDOException $e) { // error_log($e->getMessage()); header('Location: ngo_dashboard.php?status=error'); } exit();