0

i am trying to pass the variable to next page via session using PHP. below code save the last value . i want to pass the 'name' when user clicks on "More Info " link,there i will display more information about clicked Name.

<?php while ($r = $q->fetch()):
 $city=$r['city'];
 $phone=$r['phone'];
 $name=$r['name'];
 if (session_status() == PHP_SESSION_NONE) {
   session_start();
  }
 $_SESSION['name'] = $name;
 $_SESSION['phone'] = $phone;
  $_SESSION['city'] = $city;

 ?>
        <tr>
       <td><?php echo (++$i); ?></td>
            <td><?php echo htmlspecialchars($name) ?></td>
            <td><?php echo htmlspecialchars($phone); ?></td>
            <td><?php echo htmlspecialchars($city); ?></td>
      <td> <a href="more_info.php" >More Info</a></td>
        </tr>
    <?php endwhile; ?> 
Joy
  • 145
  • 2
  • 9
  • There is no good reason to declare the session start in a loop -- even if the condition makes it only declare once. It is an avoidable inefficiency. – mickmackusa May 05 '18 at 08:29
  • Your `$_SESSION` variables are being overwritten on each iteration. Using `$_SESSION` seems a poor choice unless I am misunderstanding your task. Do you want to pass all values via `$_GET`? Perhaps you could more simply pass the `id` (assumed column name) to the `more_info.php` page via `?id=$id`, then just call a `SELECT` query to find your desired _more info_ from the db. If this is what you want please edit your question. – mickmackusa May 05 '18 at 08:49
  • why using session? you can simply do More Info – Javed Sayyed May 05 '18 at 08:49
  • @Javed That may be okay ...so long as `$name` is unique in the db. Rahul has already suggested what you are suggesting. – mickmackusa May 05 '18 at 08:52
  • Possible duplicate of [How to pass variable through without form](https://stackoverflow.com/questions/36815200/how-to-pass-variable-through-without-form) – mickmackusa May 05 '18 at 09:02

1 Answers1

2

first you need to do put session_start() top of the script.and always remember $_SESSION is super global variable.no need to pass here to access session variable.simply you can access like this.

echo $_SESSION['name'];

Note:make sure session_start() should be top of the script.

if you really want to pass variable one page to another page.do something like below

<td> <a href="more_info.php?name=<?php echo $_SESSION['name']; ?>" >More Info</a></td> 

And your more_info.php.

if (isset($_GET['name'])) {
    $name=$_GET['name'];
    echo $name;
}

given answer could be solve the OP problem but.this is not good habit to use session in while loop.because you are trying to insert name in session but name could be different.i recommend to you try something like this.

Remove:

if (session_status() == PHP_SESSION_NONE) {
   session_start();
  }
 $_SESSION['name'] = $name;
 $_SESSION['phone'] = $phone;
  $_SESSION['city'] = $city;

And pass the variable like this.

<td> <a href="more_info.php?name=<?php echo $name; ?>" >More Info</a></td> 
Rahul
  • 1,617
  • 1
  • 9
  • 18