forked from FindMEmory/FindMEmory_BE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_answer.php
More file actions
32 lines (24 loc) · 947 Bytes
/
add_answer.php
File metadata and controls
32 lines (24 loc) · 947 Bytes
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
<?php
require_once __DIR__ . "/db_connect.php";
$question_id = $_POST['question_id'] ?? 0;
$author_id = $_POST['author_id'] ?? 0;
$body = $_POST['body'] ?? '';
if (!$question_id || !$author_id || empty($body)) {
echo json_encode(["status" => "error", "message" => "필수 값 없음"]);
exit;
}
$sql = "INSERT INTO answers (question_id, author_id, body, is_accepted)
VALUES (?, ?, ?, b'0')";
$stmt = $conn->prepare($sql);
$stmt->bind_param("iis", $question_id, $author_id, $body);
if ($stmt->execute()) {
// 질문 테이블의 answer_count 증가
$update = $conn->prepare("UPDATE questions SET answer_count = answer_count + 1 WHERE question_id = ?");
$update->bind_param("i", $question_id);
$update->execute();
echo json_encode(["status" => "success", "message" => "댓글 등록 성공"]);
} else {
echo json_encode(["status" => "error", "message" => $conn->error]);
}
$conn->close();
?>