Any body help me to get click value in another page using sessions
my code is

<table class="table table-condensed">
        <thead>
            <tr>
                <th>S No</th>
                <th>Student Name</th>
                <th>Standard</th>
                <th>branch</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
                    <?php
$candarr=explode(",",$cand_id_mul);
$count=count($candarr);


for($i=0;$i<$count;$i++){

$cand_id=$candarr[$i];


$rt=mysql_query("select * from  candidate where cand_id='$cand_id'")or die(mysql_error());
$count_rt=mysql_num_rows($rt);
while($row_rt=mysql_fetch_array($rt))
{
$cand_id=$row_rt['cand_id'];
$_SESSION['cand_id']=$cand_id;
echo $cand_id;



?>

            <tr>
                <td><?php echo $i;    ?></td>
                <td><?php echo $row_rt['first_name'];  ?></td>
                <td><?php echo $row_rt['standard'];  ?></td>
                <td><?php echo $row_rt['branch'];  ?></td>
                <td>
    <ul class="pager">
<li class="previous">
<?php 


  ?>
    <li><a href='preview.php?cand_id=<?php echo $cand_id; ?>' onClick="return confirm('Are you sure you want to View?')">View</a></li>
</li>
</ul>

            </tr>
                            <?php } 



                            }  ?>

        </tbody>
    </table>

Dani AI

Generated

Short version: the symptom you describe happens when a single session key is written repeatedly while building the table — each row overwrites the same session entry, so after the page finishes the session contains the last ID. When the preview page reads that session key it always sees the final value. , stop relying on a session value set during the loop; instead pass the clicked row's ID to the preview page and read that request parameter there. As suggested, be specific — here is a clear, safe pattern.

Example (preview page: read the clicked ID, validate and fetch safely):

<?php
session_start();

// prefer GET or POST; cast/validate before use
if (!empty($_GET['id'])) {
    $id = (int) $_GET['id'];        // simple sanitization
    // optionally remember it:
    $_SESSION['selected_id'] = $id;

    // use prepared statements (PDO shown)
    $pdo = new PDO($dsn, $user, $pass, $opts);
    $stmt = $pdo->prepare('SELECT * FROM students WHERE id = ?');
    $stmt->execute([$id]);
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    // handle $row or show "not found"
} else {
    // handle missing id
}
?>

Hints for the listing page

  • Make each row produce a request that carries the row ID (query string or form POST). A simple per-row form with a hidden field and a submit button guarantees the clicked ID is sent server-side.
  • Do not set the session key for each row while rendering the table; set it only when processing the click (preview page) if you truly need a session copy.

Troubleshooting checklist

  • Confirm each link/form in the generated HTML contains the correct, distinct ID (View source or Network tab).
  • Ensure session_start() runs before reading/writing $_SESSION on every page.
  • Replace deprecated mysql_* usage with PDO or mysqli prepared statements to avoid SQL injection.
  • Escape output with htmlspecialchars() when printing user data.

This approach fixes the “last ID” problem and is safer and easier to debug.

If you reword your qestion you may get more help. It is a real chore to read and doesn't explain things very well. Try and be very specific, unambiguous and use full, clear sentences.

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.