-1

So i made a dynamic table and now i want to make the table rows selectable (max 1) and send the information to the next page where u can edit the phpmyadmin tablerow that you selected through the dynamic table. But only if its not too complicated, otherwise i'll just make it editable through the dynamic table itself.

This is what i got so far:

<form action="edit.php" method="post">
    <div class="table">
        <table cellspacing="0">
            <tr class="top">
                <th>Titel</th>
                <th>Auteur</th>
                <th>ISBN13</th>
                <th>Uitgever</th>
                <th>Pagina's</th>
                <th>ID</th>
            </tr>
        <?php

        $sql = "SELECT * FROM books";
        $result = $conn->query($sql);

        if ($result->num_rows > 0){
            while($row = $result->fetch_assoc()){
                ?> 
                <tr>
                    <th><?php echo $row['title']; ?></th>
                    <th><?php echo $row['author']; ?></th>
                    <th><?php echo $row['isbn13']; ?></th>
                    <th><?php echo $row['publisher']; ?></th>
                    <th><?php echo $row['pages']; ?></th>
                    <th><?php echo $row['id']; ?></th>
                </tr>
                <?php
            }
        } else {
            echo "Sorry, maar er zijn nog geen boeken in het assortiment.";
        }

        $conn->close();
        ?>
        </table>
    </div>
<button class="continue" type="submit">Doorgaan</button>
</form>
Marnix Elling
  • 335
  • 1
  • 3
  • 15
  • You have to have a form element and button for every row. and you can add for every Form – nbk Sep 12 '19 at 22:59

1 Answers1

1

You can just add 1 more column for edit button action button like so :

<th><a href="url/editbook.php?id=<?php echo $row['id']; ?>">Edit</a></th>

This will provide a clickable link to, for example, url/editbook.php?id=1. From here you can create editbook.php page and create form to edit the content of the selected book.

In editbook.php you have to query for the selected book id. Something like this :

$sql = "SELECT * FROM books WHERE id = $_GET['id']";

I hope you understand what i'm trying to say.

P/S : Don't forget to sanitize. You can find some reference in this discussion

fazrinwiraman
  • 79
  • 1
  • 11
  • this is what i've done in edit.php (for other people to use too) ```$sql = "SELECT * FROM books WHERE id =".$_GET['id']; $result = $conn->query($sql); if ($result->num_rows > 0){ while($row = $result->fetch_assoc()){ ?>
    ul>
  • Titel
  • close(); ?> ```
    – Marnix Elling Sep 13 '19 at 13:10
  • since you are querying for one result, use [fetch_row()](https://www.php.net/manual/en/mysqli-result.fetch-row.php) instead. And its a good practice not to mix up too much PHP code with HTML code. It will make your code much cleaner and easy to maintain. – fazrinwiraman Sep 17 '19 at 23:34