P
P
Pavl-852021-07-31 18:38:46
PHP
Pavl-85, 2021-07-31 18:38:46

Why is the entry from the guest book not being deleted on the page in the browser and in the database?

Good afternoon!
There is this code:

<?php
/* Основные настройки */
define('DB_HOST', 'localhost');
define('DB_LOGIN', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'gbook');

$link = mysqli_connect(DB_HOST, DB_LOGIN, DB_PASSWORD, DB_NAME) or die('ERROR');
/* Основные настройки */

/* Сохранение записи в БД */

if($_SERVER['REQUEST_METHOD'] == 'POST'){ //проверка отправки формы
    echo 'Форма отправлена' . '<br>';

    $name = strip_tags($_POST["name"]);
    $email = strip_tags($_POST["email"]);
    $msg = strip_tags($_POST["msg"]);

    echo '<br>';

    //echo $name . ' ' . $email . ' ' . $msg;

    $res = "INSERT INTO `msgs` (name, email, msg) VALUES ('$name', '$email', '$msg')";

    mysqli_query($link, $res);
}


/* Сохранение записи в БД */

/* Удаление записи из БД */
if(isset($_GET['id']) == 'gbook' && $_GET['del'] == 'id'){
    echo 'Удаление';

    $query_del ="DELETE FROM msgs WHERE id = $id";

    mysqli_query($link, $query_del);
}

/* Удаление записи из БД */
?>
    <h3>Оставьте запись в нашей Гостевой книге</h3>

    <form method="post" action="<?= $_SERVER['REQUEST_URI']?>">
        Имя: <br /><input type="text" name="name" /><br />
        Email: <br /><input type="text" name="email" /><br />
        Сообщение: <br /><textarea name="msg"></textarea><br />

        <br />

        <input type="submit" value="Отправить!" />

    </form>
<?php
/* Вывод записей из БД */
$show = "SELECT id, name, email, msg, UNIX_TIMESTAMP(datetime) as dt FROM msgs ORDER BY id DESC";
$res_show = mysqli_query($link, $show);
//$row = mysqli_fetch_all($res_show, MYSQLI_ASSOC);

$count = 0; //всего записей

while($row = mysqli_fetch_array($res_show, MYSQLI_ASSOC)){
    $count++;
    echo 'Идентификатор: ' . $bd_id = $row['id'] . '<br>';
    echo 'Имя: ' . $bd_name = $row['name'] . '<br>';
    echo 'Email: ' . $bd_email = $row['email'] . '<br>';
    echo 'Сообщение: ' . $bd_msg = $row['msg']  .'<br>';
    echo '<a href="' . 'http://localhost/mysite.local/index.php?id=gbook&del=' .$row['id'] . '">Удалить</a>';
    echo '<br>';
    echo '<hr>';
}

echo '<p>Всего записей в гостевой книге: ' . $count . '</p>'  . '<br>';

mysqli_close($link);

?>


In this code, the part where you need to delete the entry from the guest book so that it does not exist in the database and, accordingly, from the page in the browser does not work. I.e. there is no problem with adding a record to the database and displaying it on the page in the browser. Doesn't work, exactly, deletion. When I click "Delete" a certain entry, the entry is not deleted, but only the page is refreshed without any changes, and in the address bar of the browser appears, for example, mysite.local/index.php?id=gbook&del=1 if I select the entry with id = 1. It seems that everything is logical, it seems to me, but the record is not deleted.
If you see an error in the code, please tell me where it is?
Thank you!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question