Skip to content
Advertisement

Displaying database information on different page after clicking button

I am using php and mysql to display all the user information of different users and i have a button which gets the id which will be redirected to another page and the id will be displayed in the url. What i am trying to do now is display the user information with the selected id on the redirected page

$sql = "SELECT * FROM users";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){

        while($row = mysqli_fetch_array($result)){
                echo "<div class='users-data'>";
                echo "<p>" . $row['username'] . "</p>";
                echo "<p>" . $row['full_name'] . "</p>";
                echo "<p>" . $row['age'] . "</p>";
                echo "<p>" . $row['gender'] . "</p>";
                echo "<p>" . $row['email'] . "</p>";
                echo "<p>" . $row['medical_condition'] . "</p>";
                echo "<img src=images/".$row['image'] ."/>";

          echo '<td><a href="view-user-information.php?id='.$row['id'].'"><button>View Details</button></td>';
        echo "</div>";
        }
        // Free result set
        mysqli_free_result($result);
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

Advertisement

Answer

Use this code;

   if(isset($_GET['u'])){
        $id = $_GET['u'];
        $sql = "SELECT * FROM `users` WHERE `id` = '$id'";
        $result = mysqli_query ($link,$sql);
        $row = mysqli_fetch_assoc($result);
                echo "<div class='users-data'>";
                echo "<p>" . $row['username'] . "</p>";
                echo "<p>" . $row['full_name'] . "</p>";
                echo "<p>" . $row['age'] . "</p>";
                echo "<p>" . $row['gender'] . "</p>";
                echo "<p>" . $row['email'] . "</p>";
                echo "<p>" . $row['medical_condition'] . "</p>";
                echo "<img src=images/".$row['image'] ."/>"
    }
    else
    {
        //you can redirect it to back to the previous page if no id exist in url;
        header("LOCATION:index.php"); // change the index.php to your privious page url
    }
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement