我试图将会话变量 $_SESSION['id'] 从一个页面传递到另一个页面。但是在页面中我试图将它传递给我得到这个错误 Undefined index id
这些页面位于不同的目录中,但显然在同一个父目录中,这与它有什么关系吗?
这是第一个文件
session_start();
include 'connect.php';
if(isset($_POST['category'])){
// cast the category to integer (just a little bit of basic security)
$cat = (int) $_POST['category'];
$q = "SELECT * FROM products WHERE cat=$cat AND status = 1 ORDER BY id DESC";
$result = $link->query($q);
// this will be the string that you will return into the product-data div
$returnHtml = '';
}
else if(isset($_POST['subcategory'])){
// cast the category to integer (just a little bit of basic security)
$subcat = (int) $_POST['subcategory'];
$q = "SELECT * FROM products WHERE subcat=$subcat AND status =1 ORDER BY id DESC";
$result = $link->query($q);
// this will be the string that you will return into the product-data div
$returnHtml = '';
}
// construct the html to return
while($row = mysqli_fetch_array($result)) {
$returnHtml .= "<div class='product'>";
$returnHtml .= "<a href='products.php' target='_blank''>";
$returnHtml .= "<img class='nailthumb-container'";
$returnHtml .= "src='{$row['image']}' ";;
$returnHtml .= "alt='{$row['name']}' ";
$returnHtml .= "title='{$row['name']}' />";
$returnHtml .= "</a>";
$returnHtml .= "<span class='productname1'>{$row['name']}</span>";
$returnHtml .= "<br />";
$returnHtml .= "<br />";
$returnHtml .= "<span class='productprice1'>£ {$row['price']}</span>";
$returnHtml .= "</div>";
$_SESSION['id']=$row['id'];
}
// display the html (you actually return it this way)
echo $returnHtml;
这是第二个
<?php session_start(); ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<div class="productconainer">
<?php
$id = "SELECT * FROM products WHERE id = '".$_SESSION['id']."'";
$result = $link->query($id);
while($row = mysqli_fetch_array($result)) {
echo $row['image'];
}
?>
</body>
</html>