如何通过点击href删除特定的会话记录?

我无法从会话数组中删除特定记录。当我单击删除链接时,我想删除表中的特定行。


<?php

session_start();


    if(isset($_GET["product"]) && isset($_GET["category"])){


        $nomProduct = trim($_GET["product"]);

        $category = trim($_GET["category"]);

        $_SESSION['product'][] = array(

            "nomProduct" => $nomProduct ,

            "category" =>  $category


        );


      //session_destroy();


       }

?>


    html table

              <table class="table">

                <?php foreach($_SESSION["product"] as $items) { ?>

                <tr>

                    <th width="250px"><?php echo $items['nomProduct']; ?></th>

                    <td><?php echo $items['category']; ?></td>

                    <td style="text-align: right"><a href="">Delete</a><td>

                </tr>

                <?php }?>


            </table>


慕森王
浏览 179回答 2
2回答

四季花海

修改你的 HTML<table class="table"><?php foreach($_SESSION["product"] as $key => $items) { ?>&nbsp; <tr>&nbsp; &nbsp; <th width="250px"><?php echo $items['nomProduct']; ?></th>&nbsp; &nbsp; <td><?php echo $items['category']; ?></td>&nbsp; &nbsp; <td style="text-align: right"><a href=?key="<?php echo $key; ?>">Delete</a><td>&nbsp; &nbsp;</tr>&nbsp;<?php }?></table>捕获数组键并将其从会话数组中删除。$key = filter_input(INPUT_GET, 'key');unset($_SESSION['product'][$key]);

开心每一天1111

$key=array_search($_GET['product'],$_SESSION['product']);if($key!==false)unset($_SESSION['product'][$key]);$_SESSION["product"] = array_values($_SESSION["product"]);`也许这可能会有所帮助!您需要找到密钥,因为这是一个数组。编辑:为你做了一个例子,当你点击链接时,它会从会话数组中删除名字。<?php&nbsp; &nbsp; session_start();&nbsp; &nbsp; $_SESSION["user"] = ["fname"=>"William","lname"=>"Henry" ];&nbsp; &nbsp; if(isset($_GET["delete"]))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if($_GET["key"])&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $key=$_GET["key"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset($_SESSION['user'][$key]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }?>同一页面上的 HTML<h1>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isset($_SESSION["user"]["fname"]))echo $_SESSION["user"]["fname"]." ";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isset($_SESSION["user"]["lname"]))echo $_SESSION["user"]["lname"];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ?></h1>&nbsp; &nbsp; <a href="<?php echo $_SERVER['PHP_SELF']."?delete=user&key=fname" ?>">Delete First Name</a>如果要删除姓氏 (lname),请更改链接的 href 中的 key=lname,希望此示例对您有所帮助
打开App,查看更多内容
随时随地看视频慕课网APP