Answer the question
In order to leave comments, you need to log in
Why is the last element not displayed from the mysql database?
I made two functions: one adds new user messages to the database, and the other gets them and sends them to the chat with the bot, in the second function I use the command LAST_INSERT_ID()
The problem is that the second function cannot display this most saved message
Functions:
function message_reg($username, $user_id, $message, $message_id) {
global $db;
$user_name = mysqli_real_escape_string ($username);
$chat_id = mysqli_real_escape_string ($user_id);
$messages = mysqli_real_escape_string ($message);
$messages_id = mysqli_real_escape_string ($message_id);
$query = "insert into `secret_messages` (username, user_id, message, message_id) values ('{$username}', '{$user_id}', '{$message}', '{$message_id}')";
mysqli_query($db, $query) or die ("Зарегистрировать сообщение не удалось");
}
function message_get($username, $user_id, $message, $message_id) {
global $db;
$user_name = mysqli_real_escape_string ($username);
$chat_id = mysqli_real_escape_string ($user_id);
$messages = mysqli_real_escape_string ($message);
$messages_id = mysqli_real_escape_string ($message_id);
$query = "SELECT 'message' FROM `secret_messages` WHERE id=LAST_INSERT_ID()";
mysqli_query($db, $query) or die ("Получить сообщение не удалось");
}
switch ($callback_data) {
case 'view_message':
message_get($username, $user_id, $message, $message_id);
$post = [
'chat_id' => $chat_id,
'message_id' => $message_id,
'text' => 'Текст секретного сообщения: ' . $messages,
];
sendRequest('editMessageText', $post);
break;
default: // вывод callback_data
$post = [
'chat_id' => $user_id,
'text' => $callback_data
];
sendRequest('sendMessage', $post);
break;
}
Answer the question
In order to leave comments, you need to log in
The question "why is it not displayed" is inappropriate here. The question here is more likely to be "Does this code even somehow work?" Spoiler: no.
This code is wrong in so many ways that I just don't have the words.
It was written obviously without the slightest understanding, but simply by the method "I'll substitute some code, maybe it will do."
Moreover, there is no understanding at any level - neither how the database works, nor how PHP works, nor how PHP works with the database. Not even simple worldly common sense. Why in message_get() all these $user_name = mysqli_real_escape_string ($username);?
First, using LAST_INSERT_ID, you can get the "last record" only immediately after inserting. And since in the "main code" the receipt of the message is not explicitly performed in the same branch as the entry, LAST_INSERT_ID will not work. And there is not even simple worldly logic here. If first one user added a message, and then the second, then when the "last" one is received, how does the database know whose "last" message should be shown?
To show the last post for a specific user, as correctly noted in the comments, you need to sort the table by the time it was added and select only 1 row using LIMIT.
But besides this, we need to specify for which user we receive this message.
Secondly, the mysqli_real_escape_string() function is called incorrectly, it will throw an error. And in a good way, you generally need to use not it, but prepared expressions.
Thirdly, 'message' in the query will return the word 'message', not the message itself.
Fourthly, view_message wants to show several values, but only this 'message' is selected in the request
. Fifthly, as correctly written above, the function itself neither returns anything nor even receives the information requested from the database. The function should look like this at least
function message_get($user_id) {
global $db;
$chat_id = mysqli_real_escape_string($db,$user_id);
$query = "SELECT message_id, user_id as chat_id, message as `text`
FROM `secret_messages`
WHERE user_id='$chat_id' ORDER BY id DESC LIMIT 1";
$result = mysqli_query($db, $query);
return $result->fetch_assoc();
}
$post = message_get($user_id);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question