会话变量不在文件之间传递

时间:2013-08-25 11:31:43

标签: php session

我试图将会话变量$ _SESSION ['id']从一个页面传递到另一个页面。但在页面我试图传递给我我得到这个错误 未定义的索引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'>&pound {$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>

1 个答案:

答案 0 :(得分:0)

我认为Undefined index:id就在这一行:

$_SESSION['id']=$row['id'];

如果!isSet($_POST['category'])!isSet($_POST['subcategory']) 变量 $ row 未定义。

相关问题