在我的PHP程序中,我有一些链接都链接到同一页面。它的作用是根据我的操作更新当前站点,并通过GET发送新信息或者如果我需要取消设置会话。
在同一页面的一个链接中,我调用unset($ _ SESSION ['search']),另一个我调用GET来排序信息。但是当我调用GET链接而不是未设置的链接时,我的会话变量无论如何都会被重置。
一切都在view.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']总是被重置,当我按下获取链接时,总是会调用最后一个,即使我需要它仍然存在!
答案 0 :(得分:0)
您的未设置链接结构似乎只是在“查看全部”链接的href内的“搜索”变量内联上调用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;
}