<?php
header('Content-Type: application/json');

$servername = "localhost"; // You might need to specify your database host here.
$dbname = "antenicenefather_anf";
$username = "antenicenefather_anf";
$password = "363213119.363213119";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die(json_encode(['error' => "Connection failed: " . $conn->connect_error]));
}

if (isset($_GET['id'])) {
    $id = intval($_GET['id']); // Ensure the ID is an integer

    // Fetch the required fields using a prepared statement to avoid SQL injection
    $stmt = $conn->prepare("SELECT content_2, gpt_response, gpt_response_1, gpt_response_2, gpt_response_3, gpt_response_4, gpt_response_5, gpt_response_6, gpt_response_7, gpt_response_8, gpt_response_9 FROM longform WHERE id = ?");
    $stmt->bind_param("i", $id);
    $stmt->execute();

    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        echo json_encode([
            'content_2' => $row['content_2'],
            'gpt_response' => $row['gpt_response'],
            'gpt_response_1' => $row['gpt_response_1'],
            'gpt_response_2' => $row['gpt_response_2'],
            'gpt_response_3' => $row['gpt_response_3'],
            'gpt_response_4' => $row['gpt_response_4'],
            'gpt_response_5' => $row['gpt_response_5'],
            'gpt_response_6' => $row['gpt_response_6'],
            'gpt_response_7' => $row['gpt_response_7'],
            'gpt_response_8' => $row['gpt_response_8'],
            'gpt_response_9' => $row['gpt_response_9']
        ]);
    } else {
        echo json_encode(['error' => 'No results']);
    }

    $stmt->close();
} else {
    echo json_encode(['error' => 'No ID provided']);
}

$conn->close();
?>

