<?php
include __DIR__ . '/database/search_log_config.php';

if(isset($_GET['id'])){

    $id = (int) $_GET['id'];

    // Increase click count
    $update = $log_conn->prepare("
        UPDATE sponsored_keywords 
        SET click_count = click_count + 1
        WHERE id = ?
    ");
    $update->bind_param("i", $id);
    $update->execute();
    $update->close();

    // Get redirect URL
    $stmt = $log_conn->prepare("
        SELECT redirect_url 
        FROM sponsored_keywords 
        WHERE id = ?
        LIMIT 1
    ");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $stmt->bind_result($redirectUrl);
    $stmt->fetch();
    $stmt->close();

    $log_conn->close();

    if(!empty($redirectUrl)){
        header("Location: " . $redirectUrl);
        exit;
    }
}
?>