Answer the question
In order to leave comments, you need to log in
Why is an empty string being passed to the DB instead of the user's response?
Hello, there was a problem that, when editing an old message, instead of the user's response to prompt() with a new message, an empty string is written to the database.
if ($_GET['act'] == 'edit' && $_GET['id']) {
$id = $_GET['id'];
// берем из БД старое сообщение пользователя
$stm = $dbh->prepare("SELECT `message` FROM `messages` WHERE `id` = '$id' ");
$stm->execute();
$oldMessages = $stm->fetch();
$oldMessage = $oldMessages[0];
// спрашиваем у пользователя новое сообщение
function prompt($oldMessage){
echo("<script type='text/javascript'> var answer = prompt('Edit message', '$oldMessage'); </script>");
$editMessage = "<script type='text/javascript'> document.write(answer); </script>";
return($editMessage);
}
// получаем новое значение
$editMessage = prompt($oldMessage);
// заменяем кавычки
$editMessage=str_replace("'",''',$editMessage);
$editMessage=str_replace('"','"',$editMessage);
if ($editMessage){
// вставляем новое сообщение пользователя
$stm = $dbh->prepare("UPDATE `messages` SET `message` = '$editMessage' WHERE id = '$id' ");
$stm->execute(); ?>
<script>
//уведомление об успешном редактировании
alert("Editing was successfull");
location.replace("//stasnepidoras.com/config/chat_room.php");
</script>
<?php }
}
Answer the question
In order to leave comments, you need to log in
This is very funny code.
PHP doesn't work the way you imagine.
Read here phpfaq.ru/newbie/na_tanke
Firstly, it is better to edit the data using the POST method. Second, you are using prepared queries incorrectly .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question