-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate_question.php
More file actions
68 lines (58 loc) · 1.63 KB
/
update_question.php
File metadata and controls
68 lines (58 loc) · 1.63 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
require_once __DIR__ . '/db_connect.php';
$question_id = $_POST['question_id'] ?? '';
$title = $_POST['title'] ?? '';
$body = $_POST['body'] ?? '';
// 필수값 체크
if ($question_id === '' || $title === '' || $body === '') {
echo json_encode([
"success" => false,
"msg" => "필수값 누락"
]);
exit;
}
if (array_key_exists('keyword_id', $_POST)) {
// 키워드 제거
if ($_POST['keyword_id'] === '') {
$sql = "
UPDATE questions
SET title = ?, body = ?, keyword_id = NULL, updated_at = NOW()
WHERE question_id = ?
";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssi", $title, $body, $question_id);
} else {
// 키워드 변경
$keyword_id = (int)$_POST['keyword_id'];
$sql = "
UPDATE questions
SET title = ?, body = ?, keyword_id = ?, updated_at = NOW()
WHERE question_id = ?
";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssii", $title, $body, $keyword_id, $question_id);
}
} else {
// 키워드 유지 (POST에 아예 안 온 경우)
$sql = "
UPDATE questions
SET title = ?, body = ?, updated_at = NOW()
WHERE question_id = ?
";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssi", $title, $body, $question_id);
}
$result = $stmt->execute();
if ($result) {
echo json_encode([
"success" => true,
"msg" => "수정 완료"
]);
} else {
echo json_encode([
"success" => false,
"msg" => $conn->error
]);
}
$stmt->close();
$conn->close();