doutuo3899 2016-11-23 23:22
浏览 11
已采纳

使用PHP Sessions将数据从一个页面传递到另一个页面

I'm using sessions in PHP. At the top of both pages, I do a session_start(); before anything else.

Later on, information is pulled from a database that is used to populate the page with information. Right now I'm only using 20 different items. The first page iterates through the database and gives me output that I expect. Once you select an image, it goes to another page that should have more information on that object. The problem is, the new page always shows the last object in the database. I'll post relevant code and hope that someone can point out my failure.

<?php

//initial page with list of objects

session_start();

$_SESSION['listingID'] = $listingID;


?>

<!DOCTYPE HTML>
<html>
<head>
    <title>Some Title</title>
     <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link rel="stylesheet" href="main.css" />

</head>
<body>

<!-- Wrapper -->
<div id="wrapper">

    <!-- Header -->
    <header id="header">
        <h1><a href="#" target="__blank">Some H1 that works fine</h1>
        <nav>
            <ul>
                <li><a href="#" target="__blank">Some link</a></li>
            </ul>
        </nav>
    </header>
</div>



</body>
</html>

<?php

try {
    $dbh = new PDO("mysql:host=$dbservername;dbname=$dbname", $dbusername, $dbpassword);
    //echo "Connection is successful!<br/>";
    $sql = "SELECT * FROM $tablename";
    $users = $dbh->query($sql);



    foreach ($users as $row) {
    extract($row);  
    $_SESSION['listingID'] = $listingID;
    echo "THIS IS listingID ". $listingID;
    echo '<div id="main" style="margin-bottom: 2em;">';
    echo '<article class="thumb">';
    echo '<a href="lake_full.php?'.$listingID.'" class="image"><img style="width:100%;height:100%;" src="' . $image . '" /></a>';
    echo "<h2>$address</br>$city, $state $zip</br>$$asking_price</h2>";
    echo '</article>';
    echo '</div>';  



   } // end foreach

$dbh = null;
}
catch (PDOexception $e) {
    echo "Error is: " . $e-> etmessage();
}


?>

The following code is what I'm using on the new page that should bring over the 'id' or something associated with the image selected on the first page.

 <?php

//lake_full.php
session_start();

$listingID = $_SESSION['listingID'];
echo "ID IS ".$listingID;

?>


    <!DOCTYPE HTML>
    <html>
    <head>
        <title>Some Title</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />

        <link rel="stylesheet" href="main.css" />

    </head>
    <body>

    <!-- Wrapper -->
    <div id="wrapper">

        <!-- Header -->
        <header id="header">
            <h1><a href="#">Some H1</h1>
            <nav>
                <ul>
                    <li><a href="#" target="__blank" class="icon fa-info-circle">Some Link</a></li>
                </ul>
            </nav>
        </header>



    </body>
    </html>

<?php

try {
    $dbh = new PDO("mysql:host=$dbservername;dbname=$dbname", $dbusername, $dbpassword);
    //echo "Connection is successful!<br/>";
    $sql = "SELECT listingID FROM $tablename";
    $users = $dbh->query($sql);

    echo "THIS ID IS ". $mls;

        echo '<div id="main" style="margin-top: 2em;">';
        echo '<article class="thumb">';
        echo '<a href="#" class="image"><img style="width:100%;height:100%;" src="' . $image . '" /></a>';
        echo "<h2>$address</br>$city, $state $zip</br>$$asking_price</h2>";
        echo '</article>';
        echo '</div>';

    $dbh = null;
}
catch (PDOexception $e) {
    echo "Error is: " . $e-> etmessage();
}



?>
  • 写回答

1条回答 默认 最新

  • dougan4884 2016-11-23 23:52
    关注

    You are currently overwriting $_SESSION['listingID'] on each iteration on your loop, which means that it will always only contain the last item.

    Instead of using sessions, you should use query strings.

    Change the following row in your first file:

    echo '<a href="lake_full.php?'.$listingID.'" class="image"><img style="width:100%;height:100%;" src="' . $image . '" /></a>';
    

    to

    echo '<a href="lake_full.php?listingID='.$listingID.'" class="image"><img style="width:100%;height:100%;" src="' . $image . '" /></a>';
    

    Now on lake_full.php, instead of using sessions, you can now get the ID using the $_GET-super global:

    if (!isset($_GET['listingID'])) {
        // If we didn't get an ID, we can't continue, so stop the script
        die('Invalid listing ID');
    }
    
    $listingID = $_GET['listingID'];
    
    // ... the rest of the code...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: