开发者

How to pass along specific SESSION information from one php page to another?

I have trouble to figure out a php problem described below.

I have one PHP page that lists several users in a table (the list is generated by PHP). Next to every user, there is a "remove" button. By clicking this button, the page will redirect to a new removal confirmation page, and therefore we need to pass along information about the specifically chosen user.

My idea was to use a $_SESSION array that stores all the information about the chosen user (e.g. first, last name, user ID etc.), but I cannot figure out how the array should store the information for one specific user. Right now I'm using a while loop to generate the list of users:

      while ($row = mysqli_fetch_array($data)) { 
  // Display the user data
  echo '<td><strong>' . $row['first_name'] . '</strong></td>';
  echo '<td><strong>' . $row['last_name'] . '</strong></td>';
  echo '<td>' . $row['nickname'] . '</td>';
  ...

  echo '<td><a href="remove.php">Remove</a>'; // button should开发者_StackOverflow be here
  echo '</td></tr>';
  }
 echo '</table>';

How should I make sure that THAT particular Remove link fills the $_SESSION array with that user's information? Right now, the while loop only creates a bunch of identical remove buttons that do not differentiate between the different users listed in the table. Any help is appreciated!


Using a session is a bad approach in this situation. You probably just want to pass the user id to the removal confirmation page by appending the user id to the linked url:

echo '<td><a href="remove.php?user_id='.$row['id'].'">Remove</a>';

On the removal confirmation page, you can now access the passed user id with $_GET["user_id"]


If you are redirecting to a new page anyway- could the 'button' not simply include a reference/id to the record and pass that as part of a GET/POST request to the confirmation page...which then queries the DB for the desired info?

One way would be to have something along the lines of:

  // Display the user data
  echo '<td><strong>' . $row['first_name'] . '</strong></td>';
  echo '<td><strong>' . $row['last_name'] . '</strong></td>';
  echo '<td>' . $row['nickname'] . '</td>';
  echo '<td><form action="confirmationpage.php" method="get"><input type="hidden" value ="'.$row['record_id'].'" name='record_id' /><input type="submit" /> </form>'</td>';

So each row in the table has a button, and a hidden field with the id of that record- when you click the submit button you go to the confirmation page and the record id is passed, you can then grab this id to query the DB for the relevant data.

Alternatively, instead of using a form you could also use a hyperlink (or hyperlinked button image)


Load your database results that you need into your global $_SESSION variable

i.e.

session_start();  
$_SESSION['userData'] = $queryResultsAsAnArray;

Then, reference that in your html, such as:

echo '<td><strong>' . $_SESSION['userData']['first_name'] . '</strong></td>';

Or I prefer to load the super global session data in a new variable and use that, like: ]

  $userData = $_SESSION['userData'];
echo '<td><strong>' . $userData['first_name'] . '</strong></td>';

PHP Session Reference


No - session storage should not be used for transaction data. Even if you can ensure that users do not use the back and forward buttons, even if you can ensure that they will not open multiple windows, your code will get tied in knots.

It's not hard to...

echo '<td><a href="remove.php?uid=' . $row['id'] . '">Remove</a>';

is it?

Although really, that kind of operation should be done in a POST rather than a GET (you can have lost of forms on the page, or a single form and javascripts to write a hidden filed and submit the form).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜