Initial commit: 교육 프로젝트 배포
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
<?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' => '잘못된 요청입니다.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$goal_code = isset($_POST['goal_code']) ? trim($_POST['goal_code']) : '';
|
||||
$seqRaw = isset($_POST['seq']) ? trim((string)$_POST['seq']) : '';
|
||||
$seq = ($seqRaw !== '' ? (int)$seqRaw : null);
|
||||
$title = isset($_POST['title']) ? trim($_POST['title']) : '';
|
||||
$title2 = isset($_POST['title2']) ? trim($_POST['title2']) : '';
|
||||
$is_active = isset($_POST['is_active']) ? '1' : '0';
|
||||
|
||||
if ($goal_code === '') {
|
||||
echo json_encode(['success' => false, 'message' => '학습목표 코드가 필요합니다.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
if ($seq === null) {
|
||||
if ($seq === null) {
|
||||
// 신규 등록: seq를 순차증감(최대 3개)
|
||||
$stmt = $pdo->prepare("SELECT COALESCE(MAX(seq), 0) FROM edu_recommended_goals WHERE goal_code = :g");
|
||||
$stmt->execute([':g' => $goal_code]);
|
||||
$nextSeq = (int)$stmt->fetchColumn() + 1;
|
||||
if ($nextSeq > 3) {
|
||||
echo json_encode(['success' => false, 'message' => '추천이유는 최대 3개까지만 등록할 수 있습니다.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$ins = $pdo->prepare("INSERT INTO edu_recommended_goals (goal_code, seq, title, title2, is_active, created_by, created_at, updated_by, updated_at)
|
||||
VALUES (:g, :s, :t, :t2, :a, 'admin', NOW(), 'admin', NOW())");
|
||||
$ins->execute([':g' => $goal_code, ':s' => $nextSeq, ':t' => $title, ':t2' => $title2, ':a' => $is_active]);
|
||||
echo json_encode(['success' => true, 'seq' => $nextSeq]);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
// 수정: 존재하면 UPDATE, 없으면 INSERT(지정 seq)
|
||||
$sql = "INSERT INTO edu_recommended_goals (goal_code, seq, title, title2, is_active, created_by, created_at, updated_by, updated_at)
|
||||
VALUES (:g, :s, :t, :t2, :a, 'admin', NOW(), 'admin', NOW())
|
||||
ON DUPLICATE KEY UPDATE
|
||||
title = VALUES(title),
|
||||
title2 = VALUES(title2),
|
||||
is_active = VALUES(is_active),
|
||||
updated_by = 'admin',
|
||||
updated_at = NOW()";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([':g' => $goal_code, ':s' => $seq, ':t' => $title, ':t2' => $title2, ':a' => $is_active]);
|
||||
|
||||
echo json_encode(['success' => true, 'seq' => $seq]);
|
||||
} catch (PDOException $e) {
|
||||
echo json_encode(['success' => false, 'message' => 'DB 에러: ' . $e->getMessage()]);
|
||||
}
|
||||
exit;
|
||||
Reference in New Issue
Block a user