File size: 1,434 Bytes
0dff816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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()]);
}
?>