Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php $\会话保留旧值_Php_Mysql - Fatal编程技术网

Php $\会话保留旧值

Php $\会话保留旧值,php,mysql,Php,Mysql,我正在编写PHP,我的$会话变量有问题。它应该得到一个新的值,但它保留了他的旧值而不是新值 <?php session_start(); require_once("inc/config.inc.php"); require_once("inc/functions.inc.php"); include("templates/header.inc.php");// simple the header, nothing important ;) ?> <?php $statem

我正在编写PHP,我的$会话变量有问题。它应该得到一个新的值,但它保留了他的旧值而不是新值

<?php
session_start();
require_once("inc/config.inc.php");
require_once("inc/functions.inc.php");
include("templates/header.inc.php");// simple the header, nothing important ;)

?>

<?php
$statement = $pdo2->prepare("SELECT * FROM fragen");
$result = $statement->execute();
while($row = $statement->fetch()) {
        echo '<a class="kein" href="forum_forum.php"><div class="forum"><div class="forum_Titel">' ,"Titel:  ".$row['fragen_title'].'</div>',
        "<br><br>",'<div class="forum_Subt  itel">', "Subtitel:  " .$row['fragen_subtitle'].'</div>',
        "<br><br>",'<div class="forum_Content">',"Text:  ".$row['fragen_content'].'</div>',
        "br><br>",'<div>'.$row['fragen_id'].'</div>',
        "<br><br>",'<div class="forum_Upload-date">',"Upload-Datum:  ".$row['fragen_date'].'</div>',
        '</div></a>',
        $chosen_one = $row['fragen_id'];    //the variable $chosen_one should get the value of the id   
        $_SESSION['chosen_one'] = $chosen_one; // and here I want to make this variable global for the next site.
}
?>
<?php 
include("templates/footer.inc.php");
?>

开始之前,请尝试将其添加到您的注销页面或此页面的顶部

 $_SESSION['xxx'] = ''; //for each variable you want to unset.
session_unset();
session_destroy();

问题出在你的风格上。 如果$\u SESSION['selected\u one']==NULL,会发生什么情况

您正在使用获取所有记录

$statement = $pdo2->prepare("SELECT * FROM fragen");
然后在while循环中,将当前元素的行值设置为会话变量

$chosen_one = $row['fragen_id'];    //the variable $chosen_one should get the value of the id   
    $_SESSION['chosen_one'] = $chosen_one; // and here I want to make this variable global for the next site.
结果是,您将始终在会话变量中保留结果集的最后一项

这里的问题是如何获得chosenid


如果它有助于

$\u会话['selected\u one']
始终存储最后的
$行['fragen\u id']
值,请标记为答案。你知道吗?前3个
块都在同一个脚本文件中吗?你想在会话中存储多少
$row['fragen\u id']
?只返回最后一行或全部,然后在该循环中返回以下哪一行将某些内容存储到会话中?你说的是哪把不变的钥匙?您是否检查过代码是否会运行到更改某些内容的分支中?您知道我如何修复此问题吗?问题是您正在尝试归档什么?通常,您为希望接收的数据构建查询和筛选,而不是获取所有记录$statement=$pdo2->prepare(“SELECT*FROM fragen,其中fragen_id=$myid”);//快速代码,您应该检查sql注入现在您需要从代码中的某个地方获取$myid,也许您有一个前端,可以在其中选择某个内容,然后如果您只需要一行,您可以从那里传递id(例如,如果您按id筛选它)你可以直接获取那一行,而不是使用循环。我想在一个网站中创建一个论坛,如果你按下一个标题,洞的内容就会在另一个页面中打开。所以我必须实现,我可以将每个标题与它的内容链接起来…很难用简短的形式来描述,但是你通常会在所有的板上进行选择,然后在你的获取循环中,你用它的id呈现每个论坛,所以你可以建立像boards.php?boardid=$id这样的链接,如果你选择了它,您需要选择具有相关板id的所有主题,并以相同的方式呈现所有主题。。也许你应该查阅一些教程来获得基本的想法,然后编写自己的教程。。没有检查本教程,但它可能会帮助您了解基本概念
$statement = $pdo2->prepare("SELECT * FROM fragen");
$chosen_one = $row['fragen_id'];    //the variable $chosen_one should get the value of the id   
    $_SESSION['chosen_one'] = $chosen_one; // and here I want to make this variable global for the next site.