gpt4 book ai didi

php - 使用 PHP Session 将数据从一个页面传递到另一个页面

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:42 25 4
gpt4 key购买 nike

我在 PHP 中使用 session 。在这两个页面的顶部,我做了一个 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'],这意味着它将始终只包含最后一项。

您应该使用查询字符串而不是使用 session 。

更改第一个文件中的以下行:

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,而不是使用 session :

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

关于php - 使用 PHP Session 将数据从一个页面传递到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40775758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com