为什么我不设置会话变量';t调用取消设置的href


Why do my session variables unset when I don't call the href that unsets it

在我的PHP程序中,我有几个链接,它们都链接到同一个页面。它所做的是根据我的操作更新当前网站,我通过GET发送新信息,或者如果我需要取消设置会话。

在同一页面的一个链接中,我调用unset($_SESSION['search']),而在另一个链接上,我调用GET对信息进行排序。但是,当我调用GET链接而不是未设置的链接时,我的会话变量无论如何都会被重置。

一切都在视图中完成。php

这是我未设置的链接

<a href="view.php<?php unset($_SESSION['search']); ?>">View All</a>

我的GET链接

<th><a href="view.php?id=<?php echo 'id';?>">ID</a></th>
<th><a href="view.php?id=<?php echo 'itemName';?>">Item Name</a></th>
<th><a href="view.php?id=<?php echo 'description';?>">Description</a></th>
<th><a href="view.php?id=<?php echo 'supplierCode';?>">Supplier</a></th>
<th><a href="view.php?id=<?php echo 'cost';?>">Cost</a></th>

当我调用未设置的链接时,我需要它来重置会话变量。但是当我调用get链接时,我需要保留会话变量。

我该怎么做才能做到这一点?

调用那些链接时我的php操作

if (isset($_SESSION['search'])) {
    $sql_query = "SELECT * FROM inventory WHERE description LIKE '%".$_SESSION['search']."%' ORDER BY ".$id." ASC";
    $check = true;
}

else if ($_POST && !(Trim($_POST['search']) === '')) {
    $search = $link->real_escape_string($_POST['search']);
    $_SESSION['search'] = $search;
    $sql_query = "SELECT * FROM inventory WHERE description LIKE '%".$_SESSION['search']."%' ORDER BY ".$id." ASC";
    $check = true;
}
else {
    $sql_query = "SELECT * FROM inventory ORDER BY ".$id." ASC";
}

因为每当我重定向回view.php时,$_SESSION['search']总是重置的,所以当我按下get链接时,即使我需要它,最后一个else也总是被调用!

您的未设置链接结构似乎只是在href的"View All"链接内的"search"变量内联中调用unset()。不管怎样,这都会使它不被设置为"搜索"。像这样的东西似乎对你有用:

<a href="view.php?unset=1">View All</a>

然后在php操作中:

if ($_GET['unset'] == 1) {
        unset($_SESSION['search']);
}
if (isset($_SESSION['search'])) {
        $sql_query = "SELECT * FROM inventory WHERE description LIKE '%".$_SESSION['search']."%' ORDER BY ".$id." ASC";
        $check = true;
}