Skip to content
Advertisement

How to add a link to edit and delete each row in a table formulated using php

I have written php code that outputs a php table and for each row, I want to be able to add an extra column to an edit and delete link (CRUD functionality). Currently I have been able to add the extra column but I am unsure on how to actually add the edit and update links in each row. My table currently looks like this. [1]: https://i.stack.imgur.com/nwFzQ.png

The table is made using html code and the data is received using php code that is linked to another file. The following is the html code,

      <table class="content-table">
    <thead>
        <tr>
          <th>Cashflow</th>
          <th>Amount</th>
          <th>Description</th>
          <th colspan="2">Action</th>
        </tr>
      </thead>
      <tbody>
        <?php include_once('data.php'); ?>

      </tbody>
  </table>

The following is the php code (data.php):

      while ($data = mysqli_fetch_assoc($output)) {
    echo "<tr><td>". $data["cashflow"] ."</td><td>". $data["amount"] ."</td><td>". $data["description"] ;
  }

Advertisement

Answer

You need some ID from database example: $data[“id”]

    while ($data = mysqli_fetch_assoc($output)) {
    echo "<tr>";
    echo "<td>". $data["cashflow"] ."</td>";
    echo "<td>". $data["amount"] ."</td>";
    echo "<td>". $data["description"]."</td>";
    echo '<td><a href="URL TO EDIT?id='.$data["id"].'">Edit</a></td>';
  }
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement