54 lines
1.8 KiB
PHP
54 lines
1.8 KiB
PHP
<?php
|
|
session_start();
|
|
require_once '../includes/db.php';
|
|
|
|
if (!isset($_SESSION['admin_logged_in'])) {
|
|
echo json_encode(['success' => false, 'message' => 'Unauthorized']);
|
|
exit;
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
try {
|
|
// Find CIDs that have duplicates
|
|
$stmt = $pdo_bot->query("
|
|
SELECT cid, COUNT(*) as c
|
|
FROM line_staff_register
|
|
GROUP BY cid
|
|
HAVING c > 1
|
|
");
|
|
$duplicates = $stmt->fetchAll();
|
|
|
|
$deleted_total = 0;
|
|
|
|
foreach ($duplicates as $dup) {
|
|
$cid = $dup['cid'];
|
|
|
|
// Get all records for this CID ordered by date DESC, then id DESC
|
|
$stmt = $pdo_bot->prepare("
|
|
SELECT id
|
|
FROM line_staff_register
|
|
WHERE cid = ?
|
|
ORDER BY date DESC, id DESC
|
|
");
|
|
$stmt->execute([$cid]);
|
|
$records = $stmt->fetchAll();
|
|
|
|
if (count($records) > 1) {
|
|
// Keep the first one (most recent date)
|
|
$keep_id = $records[0]['id'];
|
|
|
|
// Delete the rest
|
|
for ($i = 1; $i < count($records); $i++) {
|
|
$del_stmt = $pdo_bot->prepare("DELETE FROM line_staff_register WHERE id = ?");
|
|
$del_stmt->execute([$records[$i]['id']]);
|
|
$deleted_total++;
|
|
}
|
|
}
|
|
}
|
|
|
|
echo json_encode(['success' => true, 'deleted_count' => $deleted_total, 'message' => "ลบข้อมูลที่ซ้ำซ้อนสำเร็จ จำนวน $deleted_total รายการ"]);
|
|
} catch (Exception $e) {
|
|
echo json_encode(['success' => false, 'message' => 'Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|