简体   繁体   中英

Search results are not returning from sql query using search button

I'm trying to get a search functionality working. When the user clicks in a box and searches something the page should be able to display the results with the word included, otherwise there is a simple message that no result could be found. At the moment nothing is displaying (no errors either) and I do not know why.

Here is what I have so far. Any help would be appreciated.

1) On my index.php page there is the search box and search button.

<li>
    <div id = "search">
    <form id = "search-form" action="search_results.php" method="post">
    <input type="text" name= "find" placeholder= "Search..." />
    <input type="button" name = "search" value="search" />
    </form>
</div> 
</li>

2) When the user types what they want, they are taken to search_results.php where the searching happens. A list of product names should be returned if the search was successful.

<div class = "main-content">
    <h1> Search Results </h1>
    <?php
    include 'dbconnect.php';
    $output ='';
    if (isset($_get['find'])){
    $searchq = $_get['find'];

    $query = mysqli_query($con, "SELECT * FROM products WHERE prodname LIKE '%". $searchq . "%'");
    $count = mysqli_num_rows($query);
    if($count == 0){
    echo "There was no search results !";
}
    else{
    while($row = mysqli_fetch_array($query)){
    $prodname = $row['prodname'];
    $output ='<div> '.$prodname.'</div>';
    echo $output;
}
}
} // end of outer if
    mysqli_close($con);
?>
</div>

You are doing post and using the $_get in your search_results.php file which is wrong method.

As you are using the post method in your action you should get the values accordingly

ie,

if (isset($_POST['find'])){
    $searchq = $_POST['find'];
#Your If Condition
}
else
{
#Your Else Part
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM