douyinzha5820 2013-08-25 11:31
浏览 46

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

Im trying to pass a session variable $_SESSION['id'] form one page to another. But in the page im trying to pass it to I get this error Undefined index id

The pages are in different dirs but in the same parent dir obviously, would this have anything to do with it?

This is the first file

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;

This is the second

 <?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条回答 默认 最新

  • dongxian8858 2013-08-25 11:36
    关注

    I think Undefined index: id is on this line:

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

    if !isSet($_POST['category']) and !isSet($_POST['subcategory']) variable $row is not defined.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效