false, 'message' => '잘못된 요청입니다.']); exit; } $content_id = isset($_POST['content_id']) ? trim($_POST['content_id']) : ''; if ($content_id === '') { echo json_encode(['success' => false, 'message' => '콘텐츠 ID가 필요합니다.']); exit; } try { // 1. 제약사항 검사: 학습이력(edu_learning_histories) 또는 찜목록(edu_content_wishlist) 확인 $check_stmt = $pdo->prepare(" SELECT (SELECT COUNT(*) FROM edu_learning_histories WHERE content_id = :content_id1) as history_count, (SELECT COUNT(*) FROM edu_content_wishlist WHERE content_id = :content_id2) as wishlist_count "); $check_stmt->execute([ ':content_id1' => $content_id, ':content_id2' => $content_id ]); $check_result = $check_stmt->fetch(PDO::FETCH_ASSOC); if ($check_result['history_count'] > 0 || $check_result['wishlist_count'] > 0) { echo json_encode(['success' => false, 'message' => '학습이력이나 찜 목록에 존재하는 콘텐츠는 삭제할 수 없습니다.']); exit; } // 2. 연관 데이터 삭제를 위한 트랜잭션 시작 $pdo->beginTransaction(); // 키워드 삭제 $stmt_keywords = $pdo->prepare("DELETE FROM edu_content_keywords WHERE content_id = :content_id"); $stmt_keywords->execute([':content_id' => $content_id]); // 포스트잇(메모) 삭제 $stmt_memos = $pdo->prepare("DELETE FROM edu_content_memos WHERE content_id = :content_id"); $stmt_memos->execute([':content_id' => $content_id]); // 본 콘텐츠 삭제 $stmt_content = $pdo->prepare("DELETE FROM edu_contents WHERE content_id = :content_id"); $stmt_content->execute([':content_id' => $content_id]); // 3. 트랜잭션 커밋 $pdo->commit(); echo json_encode(['success' => true]); } catch (PDOException $e) { // 오류 발생 시 롤백 if ($pdo->inTransaction()) { $pdo->rollBack(); } echo json_encode(['success' => false, 'message' => '삭제 실패: ' . $e->getMessage()]); }