Jump to content

I want to extract one row (data for one person with his ID, Image, name, age, address) individually and display it in a separate webpage for each user.


Almari

Recommended Posts

I am learning PHP and stuck at a point where I want to extract one row (data for one person with his ID, Image, name, age, address etc) from the table individually and display it in a separate webpage for each user upon clicking View button. I have tried different solutions but failed. Please help.

Following is my table code from where I need to extract data for individual person:

<?php 
include("connection.php"); 
error_reporting(0);

$query = "SELECT * FROM users";
$data = mysqli_query($conn, $query);

$total = mysqli_num_rows($data);


if($total != 0)
{
    
?>
<h1 align="center">Consultation Console</h1>
<table border="2" cellspacing="2" width="100%" cellpadding="3px">
    <tr>
    <th width="2%">ID</th>
    <th width="10%">Reports</th>
    <th width="10%">Full Name</th>
    <th width="6%">D.O.B</th>
    <th width="2%">Gender</th>
    <th width="2%">Marital Status</th>
    <th width="1%">CNIC</th>
    <th width="8%">Address</th>
    <th width="3%">Phone</th>
    <th width="12%">Complaint</th>
    <th width="8%">History</th>
    <th width="5%">Operations</th>
    </tr>

<?php
    while($result = mysqli_fetch_assoc($data))

        echo "<tr>
            <td>".$result[id]."</td>
            <td> <img src= '".$result[ptreports]."' height='50px' width='50px'> </td>
            <td>".$result[fullname]."</td>
            <td>".$result[bday]."</td>
            <td>".$result[gender]."</td>
            <td>".$result[marital]."</td>
            <td>".$result[cnic]."</td>
            <td>".$result[address]."</td>
            <td>".$result[phone]."</td>
            <td>".$result[complaint]."</td>
            <td>".$result[history]."</td>
            
            <td><a href='newdisplay.php?id=$result[id]'><input type='submit' value='View' class='update'><a/>

            <td><a href='update.php?id=$result[id]'><input type='submit' value=' Update' class='update'><a/>

            <a href='delete.php?id=$result[id]'><input type='submit' value='Delete' class='delete' onclick = 'return checkdelete()'><a/></td>
            </tr>";
}
?>
</table>

<script>

    function checkdelete()
    {
        return confirm('Are you sure you want to delete this record ?');
    }
</script>

 

Link to comment
Share on other sites

<?php
include 'connection.php';

$qry = "select * from users";
$rs = mysqli_query($conn, $qry);
$getRowAssoc = mysqli_fetch_assoc($rs);
//while($getRowAssoc = mysqli_fetch_assoc($rs))
        echo "<tr>
            <td>".$getRowAssoc['id']."</td>
            <td> <img src= '".$getRowAssoc['ptreports']."' height='50px' width='50px'> </td>
            <td>".$getRowAssoc['fullname']."</td>
            <td>".$getRowAssoc['bday']."</td>
            <td>".$getRowAssoc['gender']."</td>
            <td>".$getRowAssoc['marital']."</td>
            <td>".$getRowAssoc['cnic']."</td>
            <td>".$getRowAssoc['address']."</td>
            <td>".$getRowAssoc['phone']."</td>
            <td>".$getRowAssoc['complaint']."</td>
            <td>".$getRowAssoc['history']."</td>
            
            <td><a href='update.php?id=$result[id]'><input type='submit' value=' Update' class='update'><a/>

            
            </tr>";

Thanks for your reply.
Last I tried this code but upon clicking "View" for any row it always displays the record for 1st row and not for that particular row.

Link to comment
Share on other sites

That was something I wanted to do once a row is fetched. I assume the issue is before it.

Even removing href the result is the same. Why is it displaying the same 1st row for any row clicked ! 

<?php

include 'connection.php';

$qry = "select * from users";
$rs = mysqli_query($conn, $qry);
$getRowAssoc = mysqli_fetch_assoc($rs);
//while($getRowAssoc = mysqli_fetch_assoc($rs))
        echo "<tr>
            <td>".$getRowAssoc['id']."</td>
            <td> <img src= '".$getRowAssoc['ptreports']."' height='50px' width='50px'> </td>
            <td>".$getRowAssoc['fullname']."</td>
            <td>".$getRowAssoc['bday']."</td>
            <td>".$getRowAssoc['gender']."</td>
            <td>".$getRowAssoc['marital']."</td>
            <td>".$getRowAssoc['cnic']."</td>
            <td>".$getRowAssoc['address']."</td>
            <td>".$getRowAssoc['phone']."</td>
            <td>".$getRowAssoc['complaint']."</td>
            <td>".$getRowAssoc['history']."</td>
            
   
            
            </tr>";

?>

 

Link to comment
Share on other sites

1 hour ago, Almari said:
<a href='newdisplay.php?id=$result[id]'><input type='submit' value='View' class='update'><a/>

When you click the "View" button you go to "newdisplay.php".

  • What is the value of the id in address bar at the top? Does it match the one you clicked?
  • What are doing with that id value that is passed to the page? (The code?)

[edit...] After a closer look at you link I see it contains a submit input. WHY? Is that taking precedence and just reloading the page?

What happens if the link is

<a href='newdisplay.php?id=$result[id]'>View<a/>
Link to comment
Share on other sites

 

You'll use the $_GET superglobal to get the url parameter.

 

if (empty($_GET['id']) || (int)$_GET['id'] < 1) {
  exit;
}
                                               
$id = (int)$_GET['id'];
                                               
// Add your code to make the mysql connection.  Since this is a common thing, you would be better to have that code in a script you 
// require_once in any script that needs the mysql connection

$stmt = $mysqli->prepare("SELECT * FROM users WHERE id = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->store_result();

$getRowAssoc = mysqli_fetch_assoc($result);
  
if ($getRowAssoc) {
  // Display the user data  
}
                              
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.