<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
require_once 'db.php';
try {
// Check if question_id is provided
if (!isset($_GET['question_id'])) {
throw new Exception("Question ID is required");
}
$questionId = $_GET['question_id'];
// Fetch medicine details for the specific question
$query = "SELECT id, medicine FROM questions WHERE id = :question_id";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':question_id', $questionId, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$result) {
throw new Exception("No question found with the given ID");
}
// Parse the JSON medicine data
$medicineData = json_decode($result['medicine'], true);
// Return the medicine details
echo json_encode([
'status' => 'success',
'question_id' => $result['id'],
'medicines' => $medicineData ?? []
]);
} catch (Exception $e) {
echo json_encode([
'status' => 'error',
'message' => $e->getMessage()
]);
}
?>