-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaccept_answer.php
More file actions
76 lines (67 loc) · 1.5 KB
/
accept_answer.php
File metadata and controls
76 lines (67 loc) · 1.5 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
69
70
71
72
73
74
75
76
<?php
require_once __DIR__ . "/db_connect.php";
$answer_id = $_POST['answer_id'] ?? 0;
$question_id = $_POST['question_id'] ?? 0;
if (!$answer_id || !$question_id) {
echo json_encode(["status" => "error", "message" => "필수 값 없음"]);
exit;
}
/*
* 0️⃣ 새로 채택될 답변의 작성자 가져오기
*/
$getAuthor = $conn->prepare("
SELECT author_id
FROM answers
WHERE answer_id = ?
");
$getAuthor->bind_param("i", $answer_id);
$getAuthor->execute();
$getAuthor->bind_result($answerAuthorId);
$getAuthor->fetch();
$getAuthor->close();
/*
* 1️⃣ 기존 채택된 답변 해제
*/
$conn->query("
UPDATE answers
SET is_accepted = 0
WHERE question_id = $question_id
");
/*
* 2️⃣ 선택한 답변 채택
*/
$accept = $conn->prepare("
UPDATE answers
SET is_accepted = 1
WHERE answer_id = ?
");
$accept->bind_param("i", $answer_id);
$accept->execute();
$accept->close();
/*
* 3️⃣ 질문 해결 처리
*/
$solve = $conn->prepare("
UPDATE questions
SET is_solved = 1
WHERE question_id = ?
");
$solve->bind_param("i", $question_id);
$solve->execute();
$solve->close();
/*
* 4️⃣ 답변 작성자 adopt_count +1 ⭐⭐⭐
*/
$updateUser = $conn->prepare("
UPDATE users
SET adopt_count = adopt_count + 1
WHERE user_id = ?
");
$updateUser->bind_param("i", $answerAuthorId);
$updateUser->execute();
$updateUser->close();
echo json_encode([
"status" => "success",
"message" => "댓글 채택 완료"
]);
$conn->close();