false, 'error' => 'Empty content']); exit; } try { $stmt = db()->prepare("INSERT INTO messages (channel_id, user_id, content) VALUES (?, ?, ?)"); $stmt->execute([$channel_id, $user_id, $content]); $last_id = db()->lastInsertId(); // Fetch message with username for the response $stmt = db()->prepare("SELECT m.*, u.username FROM messages m JOIN users u ON m.user_id = u.id WHERE m.id = ?"); $stmt->execute([$last_id]); $msg = $stmt->fetch(); echo json_encode([ 'success' => true, 'message' => [ 'username' => $msg['username'], 'content' => htmlspecialchars($msg['content']), 'time' => date('H:i', strtotime($msg['created_at'])) ] ]); } catch (Exception $e) { echo json_encode(['success' => false, 'error' => $e->getMessage()]); }