static-variables / jweb /1a /assets /api /testimonials.php
fellybikush's picture
Upload 99 files
0dff816 verified
raw
history blame
1.43 kB
<?php
// testimonials.php
require_once 'config.php';
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
try {
$pdo = getDBConnection();
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'like') {
// Handle like action
$testimonialId = filter_var($_POST['id'], FILTER_VALIDATE_INT);
if ($testimonialId) {
$stmt = $pdo->prepare("UPDATE testimonials SET helpful_count = helpful_count + 1 WHERE id = :id");
$stmt->execute([':id' => $testimonialId]);
// Get updated count
$stmt = $pdo->prepare("SELECT helpful_count FROM testimonials WHERE id = :id");
$stmt->execute([':id' => $testimonialId]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
echo json_encode(['success' => true, 'count' => $result['helpful_count']]);
exit;
}
} else {
// Get all testimonials
$stmt = $pdo->query("SELECT * FROM testimonials ORDER BY created_at DESC");
$testimonials = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode(['success' => true, 'data' => $testimonials]);
}
} catch(PDOException $e) {
echo json_encode(['success' => false, 'message' => 'Error: ' . $e->getMessage()]);
}
?>