Форма:
<form class="chat">
<input id="chat_rank" class="form-control" value="1" type="hidden" name="chat_rank" required/>
<div id="chat1" style="border: 1px solid #333; margin-bottom: 20px;"><div id="getChatMessages" style="overflow-y: auto; height: 310px;padding: 10px;"></div></div>
<div class="input-group">
<span class="input-group-btn">
<button type="reset" class="btn btn-secondary" style="background-color: #D5D4D6;" value="Reset"><i class="fa fa-refresh fa-spin" style="padding: 3px 2px 2px 2px; color: #333;" aria-hidden="true"></i></button>
</span>
<input id="chat_text" class="form-control" placeholder="Введите сообщение" name="chat_text" required/>
<span class="input-group-btn">
<button type="submit" class="btn btn-primary" name="chat_message_send" id="chat_message_send">Отправить</button>
</span>
</div>
</form>
Запрос:
function chat_message_send() {
date_default_timezone_set("Europe/Moscow");
$chat_message_text = htmlspecialchars($_POST['chat_text']);
$rank = htmlspecialchars($_POST['chat_rank']);
session_start();
$uid = $_SESSION['id'];
$name = $_SESSION['name'];
$time = date("H:i");
$avatar = get_avatar();
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
mysqli_set_charset($link, 'utf8');
if (!$link) {
die('Could not connect: ' .mysql_error());
}
$sql = "INSERT INTO chat (userId, chat_text, name, time, avatar, rank) VALUES (?, ?, ?, ?, ?, ?)";
try {
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_bind_param($stmt, "isssss", $uid, $chat_message_text, $name, $time, $avatar, $rank);
$result = mysqli_stmt_execute($stmt);
if ($result == FALSE) {
die(mysqli_error($link));
}
}
catch (Exception $e)
{
die("Failed to run query: " . $e->getMessage()); //TODO: A function to send me an email when this occurs should be made
}
mysqli_close($link);
}