簡體   English   中英

使用PHP會話將數據從一頁傳遞到另一頁

[英]Passing data from one page to another using PHP Sessions

我正在使用PHP中的會話。 在兩個頁面的頂部,我都執行一個session_start();。 首先。

稍后,從數據庫中提取信息,該數據庫用於用信息填充頁面。 現在,我只使用20種不同的物品。 第一頁遍歷數據庫,並提供期望的輸出。 選擇圖像后,圖像將轉到另一個頁面,該頁面應具有有關該對象的更多信息。 問題是,新頁面始終顯示數據庫中的最后一個對象。 我將發布相關代碼,並希望有人指出我的失敗。

<?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();
}


?>

以下代碼是我在新頁面上使用的代碼,該代碼應帶上'id'或與第一頁上所選圖像相關的內容。

 <?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();
}



?>

當前,您在循環的每次迭代上都覆蓋$_SESSION['listingID'] ,這意味着它將始終僅包含最后一項。

而不是使用會話,您應該使用查詢字符串。

在第一個文件中更改以下行:

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

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

現在在lake_full.php ,而不是使用會話,您現在可以使用$_GET -super global獲取ID:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM