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?

The session variables should be available unless the browser window is closed and/or there is session inactivity.

Maybe seeing your relevant code would provide some insight into what's going on.

This is where I set the session

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 where i try to access it in a query

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

Have you validated that you are getting the expected value from line 36?

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

maybe just echo it to the screen to make sure its assigning a value. maybe on line 37, echo $_SESSION['id'];

If I change it to put it inthe while loop then yes it echos the correct vale.
But it echos nothing in the other file!

Hi just a thought try putting the $_SESSION['id']=$row['id']; inside the while statement in your first script.

I tried that but it still shows nothing in the second script!...........

So, i know that you are concerned about being able to access the session variable between pages. This is what I suggest...create two basic pages and validate that your session variable is accessible. No additional code in these pages. Once you get that working proceed with tracking down where the problem is.

I dont see any reason why this wouldnt work for you:

Page-1

<?php
session_start();
$_SESSION['id']=1;
?>

<html>
<body>

 <a href="session-result.php">Go to page-2</a>

</body>
</html>

Page-2

<?php
session_start();
?>

<html>
<body>

<?php
echo "ID = ". $_SESSION['id'];
?>

</body>
</html>

Make sure this simple example works.

Right I'm really confused now! The adove simple example works, i cant understand why it doesnt work in my files!!!

Ignore this message I failed :3 sorry haha

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.