Jump to content

PHP - Database Searchfunction doesn't work as intented


Recommended Posts

Hello everyone,

I have a small problem here. I want to create a basic 'searchengine' for my database. Which means: people give an input X for example and this function searches every entry in the database for that input X and shows it in a table to the user.
So far, it's showing me every current entry in the database, which is what I wanted. But the problem is, that whenever I make an input for the search, nothing happens. It's as if the page didn't change at all. I took a look earlier in my variables, but I couldn't find anything.

Heres my source code. You can also find it in the attachment.

 

<?php

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

$valueToSearch = $_POST['suchfilter'];

$query = "SELECT * FROM `tabelle` WHERE CONCAT(`firstname`, `lastname`, `gender`, `adress`) LIKE'%".$valueToSearch."%'";

$search_result = filterTable($query);

}

 

else {

$query = "SELECT * FROM `tabelle`";

$search_result = filterTable($query);

}

 

function filterTable($query){

$con = mysqli_connect("localhost","root","","csvtest");

$filt_result = mysqli_query($con, $query);

return $filt_result;

echo $filterTable;

}

?>


 

<!DOCTYPE html>

<html>

    <head>

        <title>Suchfilter</title>

    </head>

 

            <body>

                    <form>

                        <input type="text" name="suchfilter" placeholder="suchwert"><br><br>

                        <input type="submit" name="search" value="Filter"><br><br>

 

                            <table>

                                    <tr>

                                        <th>firstname</th>

                                        <th>lastname</th>

                                        <th>gender</th>

                                        <th>adress</th>

                                    </tr>

                                    <?php while($row = mysqli_fetch_array($search_result)):?>

                                        <tr>

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

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

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

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

                                         </tr>

                                    <?php endwhile;?>

                            </table>

                    </form>

        

            </body>

</html>

 

I'd appreciate an answer since this is something really important to me. 

Best regards
Knives

 

source.txt

Compare this

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

$valueToSearch = $_POST['suchfilter'];

with this

<form>

If you're still unsure, try describing to yourself precisely how a <form> submits its data to the server.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.