dongqu2863 2013-12-05 02:03
浏览 30
已采纳

当我不调用取消设置它的href时,为什么我的会话变量未设置

In my PHP program I have a few links that all link to the same page. What it does is update the current site depending on my action and I send new information through a GET or if I need to unset my session.

In one link to the same page, I am calling unset($_SESSION['search']) and in the other I am calling a GET for sorting information. But when I call the GET link and not the unset link, my session variable gets reset anyways.

Everything is being done in view.php

Here is my unset link

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

My GET link(s)

<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>

When I call the unset link, I need it to reset the session variable. But when I call the get links, I need to keep the session variable.

What can I do to make that happen?

My php action when calling those links

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";

}

Because the $_SESSION['search'] is always reset whenever I redirect back to view.php, the last else is always called when I press the get links even though I need it to stay!

  • 写回答

1条回答 默认 最新

  • dongxian7471 2013-12-05 02:17
    关注

    Your unset link structure appears to just be calling unset() on the "search" variable inline, within the href of the "View All" link. That would make it unset "search" no matter what. It seems like something like this could work for you:

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

    And then in your php action:

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答