Why is my table only showing the last row entered?

Advertisements

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 :

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!

Leave a ReplyCancel reply