简体   繁体   中英

Navigate and pass values to another page in PHP (HTML)

I am new to PHP and here I have a problem navigating to another page by clicking in one of the rows of the table. I want also to pass the values of that row to the next page and display them there.

Here is my code:

<body>
<div class="wrapper">
    <h1>List of customers</h1>
    <?php 
require_once "db_settings.php";

$sql = "SELECT id, username FROM customers";
$result = $dbi->query($sql);

if ($result->num_rows > 0) {

     echo "<table><tr><th>ID</th><th>Username</th></tr>";
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<tr> <td>".$row["id"]."</td> <td>".$row["username"]." </td> </tr>";

     }
     echo "</table>";


} else {
    echo "0 results";
}

?>
</body>
</html>

You have to add "a" tag with GET link:

 while($row = $result->fetch_assoc()) {
     echo "<tr> <td>".$row["id"]."</td> <td><a href='new_url?user_id=".$row["id"]."'>".$row["username"]." </a></td> </tr>";

 }

and in the new_url page use

$user_id = $_GET['user_id']

and now you can use the id to get the user from the database again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM