Search in table PHP/SQL

Hello all :slight_smile: I tryed to code a little project in php/sql to learn it. I want to enter a name in the form, click search and get the results from the table of the database.

I got this form:

<form action="" method="POST">
    <input type="text" name="vorname" placeholder="Hier Vorname eingeben">
</form>

I got this button:

<button type="submit" name="search" id="BirdButton" class="btn btn-primary">Search</button>

And I got this php code for output:

    <?php

        $servername = "localhost";

        $username = "root";

        $password = "";

        $db = "test";

        // Create connection

        $conn = mysqli_connect($servername, $username, $password, $db);

        // Check connection

        if ($conn->connect_error) {

            die("Connection failed: " . $conn->connect_error);

        }

        ?>

<table class="table">

                <thead class="thead-dark">

                    <tr>

                        <th scope="col">ID</th>

                        <th scope="col">Vorname</th>

                        <th scope="col">Nachname</th>

                        <th scope="col">Studiengang</th>

                        <th scope="col">Note</th>

                    </tr>

                </thead>

                <tbody>

                    <?php

                    echo 'BEFORE IF ISSET';

                    if (isset($_POST['search'])) {

                        echo 'SEARCH IS SET';

                        $vorname = $_POST['vorname'];

                        $query = "SELECT * FROM 'studenten' where vorname='$vorname' ";

                        $query_run = mysqli_query($conn, $query);

                        while ($row = mysqli_fetch_array($query_run)) {

                    ?>

                            <tr>

                                <td> <?php echo $row['id'] ?></td>

                                <td> <?php echo $row['vorname'] ?></td>

                                <td> <?php echo $row['nachname'] ?></td>

                                <td> <?php echo $row['studiengang'] ?></td>

                                <td> <?php echo $row['note'] ?></td>

                            </tr>

                    <?php

                        }

                    }

                    ?>

                </tbody>

            </table>

At the moment, if i click my button nothing happens. Can you please help me :)?

Where is your form? You have no < form > tag. POST’s are all handled when you “POST” a form.
You need to add a form to your page. In the sample code you posted, you did not show us your button either. Normally, you would put all your post code at the top of the page before the < html > tag. And, load up display data. Then, if there is data, display it in PHP code inside the table.

But, my guess is you did not set up a < form > tag with a setting saying it was method of POST.

Sponsor our Newsletter | Privacy Policy | Terms of Service