E
E
Evgeny2021-09-17 05:49:41
PHP
Evgeny, 2021-09-17 05:49:41

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 ("Получить сообщение не удалось");
}


Main code:

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;
}


I don't understand why the message can't be retrieved from the string

614402381b94f125720646.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ipatiev, 2021-09-17
@fakin_kiska

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();
}

and then receive when called
$post = message_get($user_id);

G
galaxy, 2021-09-17
@galaxy

'text' => 'Text of the secret message: ' . $messages,

And what is $messages? Who gave it meaning?
how do you think message_get() returns the text of the message or anything at all?
Last but not the least: did mysqli_query(... "SELECT ..."), do it later and fetch *

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question