Files
edu/admin/bbs/recommend_keyword_save.php

50 lines
1.8 KiB
PHP

<?php
require __DIR__ . '/../../bbs/db_conn.php';
header('Content-Type: application/json; charset=utf-8');
// PDO 연결 생성
$pdo = db_conn();
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode(['success' => false, 'message' => 'Invalid request']);
exit;
}
$keywords_csv = isset($_POST['keywords']) ? trim($_POST['keywords']) : '';
$admin_id = 'admin'; // User login ID not provided in context, defaulting to 'admin'
try {
$pdo->beginTransaction();
// Set all existing active keywords to inactive first
$updateStmt = $pdo->prepare("UPDATE edu_recommend_keywords SET is_active='0', updated_at=NOW(), updated_by=:admin WHERE is_active='1'");
$updateStmt->execute([ ':admin' => $admin_id]);
if ($keywords_csv !== '') {
$keywords = explode(',', $keywords_csv);
if (count($keywords) > 2) {
echo json_encode(['success' => false, 'message' => '최대 2개의 키워드만 선택 가능합니다.']);
exit;
}
$insertStmt = $pdo->prepare("INSERT INTO edu_recommend_keywords ( keyword_code, is_active, created_by, created_at, updated_by, updated_at)
VALUES ( :k, '1', :admin1, NOW(), :admin2, NOW())
ON DUPLICATE KEY UPDATE is_active='1', updated_by=:admin3, updated_at=NOW()");
foreach ($keywords as $k) {
$k = trim($k);
if ($k !== '') {
$insertStmt->execute([
':k' => $k,
':admin1' => $admin_id,
':admin2' => $admin_id,
':admin3' => $admin_id
]);
}
}
}
$pdo->commit();
echo json_encode(['success' => true]);
} catch (Exception $e) {
$pdo->rollBack();
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}