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.


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>

 

Why are you suppressing error reporting then asking us what's wrong?

The reporting is there to help you find errors.

Use the code button when posting code.image.png.d66142fb976de9e1c3cac956ef7e2372.png  

26 minutes ago, Almari said:

I have tried different solutions but failed.

Define "failed". What is happening, or not happening?

<?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.

2 minutes ago, Almari said:
href='update.php?id=$result[id]'

Where is $result defined? Why aren't you using $getRowAssoc['id'] ?

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>";

?>

 

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/>

1. It does match the one I clicked but displays always the 1st record on 'newdisplay.php'

2. There's no further code. Thought the above code should display individual data on 'newdisplay.php', but it isn't. 😐

There is no code there to process the id that was selected, so all you are doing is repeatedly loading the same page and expecting different results by magic.

You need to get id value from $_GET['id'] and use that to

  • retrieve the data you want to display
  • then display it.

 

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  
}
                              
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.