Home  >  Q&A  >  body text

Associate variable ID with profile page

I have a page that displays some information from the database. I want to add a link to each row (say the name as a link) that I can click to jump to a page that displays the rest of the information for that row (say a profile page). I thought about creating a link that passes the id to the profile page so the profile page can get the information.

I'm sure this is simple, but I just don't get it. How can I display a link in each row that only sends the id number of that row? Because I don't want to go to each row and create a special link.

Here is the code I have:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接
if ($conn->connect_error) {
  die("连接失败: " . $conn->connect_error);
}


$sql = "SELECT id, FirstName, LastName, Phone, Email FROM Contacts";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // 输出每一行的数据
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["FirstName"]. " " . $row["LastName"]. " " . $row["Phone"]. " " . $row["Email"]. "
"; } } else { echo "0 结果"; } $conn->close(); ?>```

P粉022285768P粉022285768428 days ago757

reply all(1)I'll reply

  • P粉998100648

    P粉9981006482023-09-11 09:22:48

    Based on your example, you can do this:

    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";
    
    // 创建连接
    $conn = new mysqli($servername, $username, $password, $dbname);
    
    // 检查连接
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
    }
    
    
    $sql = "SELECT id, FirstName, LastName, Phone, Email FROM Contacts";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
        // 输出每行数据
        while ($row = $result->fetch_assoc()) {
            echo "id: " . $row["id"] . " - Name: <a href='profile.php/?id=" . $row["id"] . "> " . $row["FirstName"] . "</a> " . $row["LastName"] . " " . $row["Phone"] . " " . $row["Email"] . " <br/>";
        }
    } else {
        echo "0 结果";
    }
    
    $conn->close();
    ?>
    

    The profile.php page will use isset to check whether the id is set, and query the data based on id, similar to this: PHP & MYSQL: Select from where id= $id

    Not tested, please make sure any user-generated variables are filtered.

    reply
    0
  • Cancelreply