skip to Main Content

I am a complete beginner who has been "experimenting" with PHP for quite some time.
Actually I’m very proud of my achievement so far, but slowly but surely I’m also doubting myself, I have the following code on page no. 1 place.php

<?php
session_start(); // Starte die Sitzung
$con = mysqli_connect("localhost", "root", "", "vmaxdbweb");
$res = mysqli_query($con, "SELECT * FROM tblplatz");
// Tabellenbeginn
echo "<table class='tbl'>";
// Überschrift
echo "<th>ID</th> <th>Bezeichung</th> <th>Code</th>";
echo "<th>Notizen & Beurteilung</th> <th>Geodaten</th> <th>Details</th>";
$lf = 1;
while ($dsatz = mysqli_fetch_assoc($res))
{
// Speichere die Daten in der Sitzung mit dem Schlüssel 'pla_id'
$_SESSION["detail"] = $dsatz["pla_id"];
echo "<tr>";
echo "<td>" . $dsatz["pla_id"] . "</td>";
echo "<td>" . $dsatz["pla_bezeichnung"] . "</td>";
echo "<td>" . $dsatz["pla_code"] . "</td>";
echo "<td>" . $dsatz["pla_notiz_beurteilung"] . "</td>";
echo "<td>" . $dsatz["pla_geodaten"] . "</td>";
echo "<td><a href='content/placedetail.php'>Details</a></td>";
$lf = $lf + 1;
}
// Tabellenende
echo "</tabelle>";
mysqli_close($con);

?>
<table>
</table>

on page 2 placedetail.php I have this code

<?php
session_start();
// Hole den Wert der `pla_bezeichnung`-Spalte aus der Sitzung
$pla_bezeichnung = $_SESSION["detail"];
// Zeige die Details des jeweiligen Platzes an
echo "Der Platz heißt " . $pla_bezeichnung;
?>​

A value is also passed, but no matter which link I click in the table, it is always only a value from the last data record in the table. Go crazy slowly but surely.

I already found that the value is always overwritten because it’s in a loop. How can I position the session better?

I’ve tried everything so far

2

Answers


  1. It’s because you put the last value intoyou session variable in the line below:

    $_SESSION["detail"] = $dsatz["pla_id"];
    

    if you want to keep all IDs in session you need to put all $dsatz["pla_id"] in an array and then pass array to $_SESSION["detail"].

    To do this outside of while write:

    $_SESSION["detail"]=array()
    

    Then inside while loop:
    Array_push($_SESSION["detail"],$dsatz["pla_id"]);

    But i believe this is not a optimal way to do this, because you need to search array again in the page 2.
    A better way would be to pass IDs to each row elements onclick, then create a http request and pass it to you page 2.

    Login or Signup to reply.
  2. Your problem is that during the while loop, $_SESSION["detail"] = $dsatz["pla_id"]; overwrites the session value at each iteration of the loop. So by the time you click to go to placedetail.php, the Session contains whatever was the last value which the loop processed.

    But I don’t see why you are using the Session here, it doesn’t really make sense. If you want the placedetail.php to show a particular record, you should follow these steps:

    1. Make the link in your table (in place.php) include the relevant record ID in the URL parameters
    2. Write code in placedetail.php to receive that ID, and retrieve the rest of the details about it from the database.

    place.php:

    ...
    echo "<td><a href='content/placedetail.php?id=".$dsatz["pla_id"]."'>Details</a></td>";
    ...
    

    placedetail.php

    <?php
    // Get the selected place using ID in the URL
    $pla_id = $_GET["id"];
    
    $con = mysqli_connect("localhost", "root", "", "vmaxdbweb");
    $stmt = $con->prepare("SELECT * FROM tblplatz WHERE pla_id = ?");
    $stmt->bind_param("s", $id);
    $stmt->execute();
    $result = $stmt->get_result();
    $rows = $result->fetch_all();
    
    if (count($rows) > 0) {
      echo "Der Platz heißt " . $rows[0]["pla_bezeichnung"];
    }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search