Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my table only showing the last row entered?

I think the problem is in my loop. But I’m also checking my database.

<table class="table">
    <thead>
      <tr>
        <th scope="col">ID</th>
        <th scope="col">First Name</th>
        <th scope="col">Last Name</th>
        <th scope="col">Username</th>
        <th scope="col">Password</th>
        <th scope="col">Actions</th>
      </tr>

    </thead>
    <tbody>
      
    <?php 
        $sql = "SELECT * from dbtest.tbluser";
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            // output data of each row
            while($row = $result->fetch_assoc()) {
                $id = $row['ID'];
                $firstname = $row['First Name'];
                $lastname = $row['Last Name'];
                $username = $row['Username'];
                $password = $row['Password']; 

    }
}
    ?>

<tr>
    <td> <?php echo $id; ?> </td>
    <td> <?php echo $firstname; ?> </td>
    <td> <?php echo $lastname; ?> </td>
    <td> <?php echo $username; ?> </td>
    <td> <?php echo $password; ?> </td>
    <td><button type="button" class="btn btn-secondary">Add</button></td>
    <td><button type="button" class="btn btn-secondary">Edit</button></td>
    <td><button type="button" class="btn btn-secondary">Update</button></td>
    <td><button type="button" class="btn btn-danger">Delete</button></td>

</tr>


    </tbody>
    
  </table>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You are setting the variables in the while loop but not your output.

So all variables are overwritten until you output the table (showing only the last iteration of the loop).

Try the code below:

<table class="table">
    <thead>
      <tr>
        <th scope="col">ID</th>
        <th scope="col">First Name</th>
        <th scope="col">Last Name</th>
        <th scope="col">Username</th>
        <th scope="col">Password</th>
        <th scope="col">Actions</th>
      </tr>

    </thead>
    <tbody>
      
    <?php 
        $sql = "SELECT * from dbtest.tbluser";
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            // output data of each row
            while($row = $result->fetch_assoc()) {
                $id = $row['ID'];
                $firstname = $row['First Name'];
                $lastname = $row['Last Name'];
                $username = $row['Username'];
                $password = $row['Password']; 
    ?>

<tr>
    <td> <?php echo $id; ?> </td>
    <td> <?php echo $firstname; ?> </td>
    <td> <?php echo $lastname; ?> </td>
    <td> <?php echo $username; ?> </td>
    <td> <?php echo $password; ?> </td>
    <td><button type="button" class="btn btn-secondary">Add</button></td>
    <td><button type="button" class="btn btn-secondary">Edit</button></td>
    <td><button type="button" class="btn btn-secondary">Update</button></td>
    <td><button type="button" class="btn btn-danger">Delete</button></td>

</tr>

<?php }} ?>


    </tbody>
    
  </table>

Good luck!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading