首页 > 解决方案 > $_SESSION['message] 在使用 $_GET 时不起作用

问题描述

我遵循了 CRUD 教程(https://www.youtube.com/watch?v=3xRMUDC74Cw&ab_channel=CleverTechie)并且在我删除记录时一切正常,但删除消息。我认为按钮类危险可能由于某种原因不起作用(过时或 w/e),并试图将其更改为其他东西,但它仍然不起作用。

现在我相信这与我使用 $_GET 方法删除记录的事实有关,但我不再确定了。成功消息和更新消息都工作正常,但删除消息不是。

索引.php

<?php require_once 'process.php'; ?>

<?php if(isset($_SESSION['message'])): ?>

<div class="alert alert-<?=$_SESSION['msg_type']?>">

    <?php 
        echo $_SESSION['message'];
        unset($_SESSION['message']);
    ?>

</div>
<?php endif ?>

<div class="container">

    <?php
        $mysqli = new mysqli('localhost', 'root', '', 'dbase') or die(mysqli_error($mysqli));
        $result = $mysqli->query("SELECT * FROM data") or die($mysqli_error);
        //pre_r($result);
    ?>

    <div class="row justify-content-center">
        <table class="table">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Location</th>
                    <th colspan="2">Action</th>
                </tr>
            </thead>

            <?php while ($row = $result->fetch_assoc()): ?>

                <tr>
                    <td><?php echo $row['name']; ?></td>
                    <td><?php echo $row['location']; ?></td>
                    <td>
                        <a href="index.php?edit=<?php echo $row['id']; ?>"
                           class="btn btn-info">Edit
                        </a>
                        <a href="index.php?delete=<?php echo $row['id']; ?>"
                           class="btn btn-danger">Delete
                        </a>
                    </td>
                </tr>

            <?php endwhile; ?>
        </table>
    </div>

    <?php
        //pre_r($result->fetch_assoc());
        //pre_r($result->fetch_assoc());

        function pre_r($array) {
            echo '<pre>';
            print_r($array);
            echo '</pre>';
        }
    ?> 

    <div class="row justify-content-center">
        <form action="process.php" method="POST">
            <input type="hidden" name="id" value="<?php echo $id; ?>">
            <div class="form-group">
                <label>Name</label>
                <input type="text" name="name" class="form-control" 
                       value="<?php echo $name; ?>" placeholder="Enter your name">
            </div>
            <div class="form-group">
                <label>Location</label>
                <input type="text" name="location" class="form-control" 
                       value="<?php echo $location; ?>" placeholder="Enter your location">
            </div>
            <div class="form-group">
                <?php 
                    if ($update == true):
                ?>
                    <button type="submit" class="btn btn-info" name="update">Update</button>
                <?php else: ?>
                    <button type="submit" class="btn btn-primary" name="save">Save</button>
                <?php endif; ?>
            </div>
        </form>
    </div>
</div>

进程.php

<?php

session_start();

$mysqli = new mysqli('localhost', 'root', '', 'pdlenart') or die(mysqli_error($mysqli));

$update = false;
$id = 0;
$name = "";
$location = ""; 

if (isset($_POST['save'])) {
    $name = $_POST['name'];
    $location = $_POST['location'];

    $mysqli->query("INSERT INTO data (name, location) VALUES('$name', '$location')") or die($mysqli_error);
        
    $_SESSION['message'] = "Record has been saved!";
    $_SESSION['msg_type'] = "success";

    header("location: index.php");
}

if (isset($_GET['delete'])) {
    $id = $_GET['delete'];

    $mysqli->query("DELETE FROM data WHERE id=$id") or die($mysqli_error);

    $_SESSION['message'] = "Record has been deleted!";
    $_SESSION['msg_type'] = "danger";

    header("location: index.php");

}

if (isset($_GET['edit'])) {
    $id = $_GET['edit'];
    $update = true;
    $result = $mysqli->query("SELECT * FROM data WHERE id=$id") or die($mysqli_error);

    if (count($result)==1) {
        $row = $result->fetch_array();
        $name = $row['name'];
        $location = $row['location'];
    }
}

if (isset($_POST['update'])) {
    $id = $_POST['id'];
    $name = $_POST['name'];
    $location = $_POST['location'];

    $mysqli->query("UPDATE data SET name='$name', location='location' WHERE id=$id") or die($mysqli_error);

    $_SESSION['message'] = "Record has been updated!";
    $_SESSION['msg_type'] = "warning";

    header("location: index.php");
}

编辑:我已经通过删除 header(location: index.php); 在 if (isset($_GET['delete'])) {} 部分。不知道发生了什么或为什么。

此外,我会记下有关 SQL 注入的评论,但这就是为什么某些功能无法正常工作或只是通过删除标头(位置..)而发生的情况。

标签: php

解决方案


推荐阅读