-3

I want to store value of <td> into a session which is clicked by user. Is it possible?

Here is my code:

$sql = "SELECT * FROM users";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
    $username = $row['user_name'];
    $id = $row['id'];
    echo "<table border='1'><tr><th>Id</th><th>UserName</th></tr><tr><th>$id</th><th>$username</th></tr>";
}
session_start();
$_SESSION['username'] = $username;
Shehary
  • 9,926
  • 10
  • 42
  • 71
Harris Khan
  • 247
  • 10
  • 26
  • What error do you have in your code? try to print your session variable echo $_SESSION['username']; it works? – bicho Aug 31 '15 at 14:00
  • This will store only the last username in your database in the session. – samlev Aug 31 '15 at 14:01
  • And if you want to retrieve it from the session, you also have to call session_start() before you request it. – samlev Aug 31 '15 at 14:02
  • calling session_start() after you output anything results in an error – Benjamin Aug 31 '15 at 14:04
  • What you've got looks like a way to display user names, but you're going to need to either make the $username output a link of some sort or use JavaScript to bridge into ajax. Normal applications of PHP (output to a browser) do not allow user input mid-page. The following S.O. question is a good reference: http://stackoverflow.com/questions/871858/php-pass-variable-to-next-page – J.T. Blum Aug 31 '15 at 14:07
  • the problem is that I got last username in a session – Harris Khan Aug 31 '15 at 14:08
  • What i actually want is to get td value on another page which is clicked by users – Harris Khan Aug 31 '15 at 14:20
  • @HarrisKhan there is other way to do it without `$_SESSION` but if you wana use `$_SESSION` then you need to do it with arrays as you are dealing with while loop – Shehary Aug 31 '15 at 15:23

1 Answers1

0

This is not possible with the approach you are using because you are fetching data using while loop and it will only load last username in $_SESSION

You can do it with arrays but how you will figure out which user has which session then and as you mentioned in comment What i actually want is to get td value on another page which is clicked by users so why using $_SESSION, you can pass what ever value to another page via link href

Change your approach to this

<?php
$sql = "SELECT * FROM users";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
    $username = $row['user_name'];
    $id = $row['id'];
?>
<table border='1'>
<tr>
    <th>Id</th>
    <th>UserName</th>
    <th>Action</th>
</tr>
<tr>
    <td><?php echo $id;?></td>
    <td><?php echo $username;?></td>
    <td><a href="pagelink.php?username=<?php echo $username;?>">Go To Page With UserName</a></td>
</tr>
<?php } ?>

So here $username value passing to another page via link (or pass any other variable value you like to pass)

<td><a href="pagelink.php?username=<?php echo $username;?>">Go To Page With UserName</a></td>

And on next page pagelink.php fetch username with $_GET from link

<?php
    $username = $_GET["username"];
    echo $username;
    //Do what ever next you wana do
?>

OR if you want to load a session, do it like this

<?php session_start();
    $username = $_GET["username"];
    $_SESSION['username'] = $username;
    echo $_SESSION['username'];
    //Do what ever next you wana do
?>

Note: mysql library is deprecated, you should start using mysqli

Shehary
  • 9,926
  • 10
  • 42
  • 71