|
|
<?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') { |
|
|
|
|
|
$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]); |
|
|
|
|
|
|
|
|
$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 { |
|
|
|
|
|
$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()]); |
|
|
} |
|
|
?> |