churchtube/api/increment_views.php

28 lines
740 B
PHP

<?php
require_once '../includes/db.php';
header('Content-Type: application/json');
$video_id = (int)($_POST['video_id'] ?? 0);
if (!$video_id) {
echo json_encode(['success' => false, 'error' => 'Invalid video ID']);
exit;
}
try {
$stmt = $pdo->prepare("UPDATE videos SET views = views + 1 WHERE id = ?");
$stmt->execute([$video_id]);
// Log the play event
$v_stmt = $pdo->prepare("SELECT title FROM videos WHERE id = ?");
$v_stmt->execute([$video_id]);
$title = $v_stmt->fetchColumn();
logEvent('play', "Started watching: $title (ID: $video_id)");
echo json_encode(['success' => true]);
} catch (Exception $e) {
echo json_encode(['success' => false, 'error' => 'Database error']);
}
?>