<?php
include 'db.php'; // Ensure you have database connection

if (isset($_FILES['image']) && isset($_POST['user_id'])) {
    $user_id = $_POST['user_id'];
    $upload_dir = "uploads/";
    $image_name = basename($_FILES["image"]["name"]);
    $target_file = $upload_dir . time() . "_" . $image_name; // Unique file name

    if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
        $image_url = "https://quiztest.store/web/webb/" . $target_file; // Full URL

        // Update user record with image URL
        $stmt = $pdo->prepare("UPDATE users SET image = ? WHERE id = ?");
        if ($stmt->execute([$image_url, $user_id])) {
            echo json_encode(["success" => true, "image_url" => $image_url]);
        } else {
            echo json_encode(["success" => false, "message" => "DB Update Failed"]);
        }
    } else {
        echo json_encode(["success" => false, "message" => "Upload Failed"]);
    }
} else {
    echo json_encode(["success" => false, "message" => "Invalid Request"]);
}
?>
