PHP Dropdown with Searach bar not working - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Friday, September 9, 2022

PHP Dropdown with Searach bar not working

Below is my code. I have a PHP file that search three tables in a database. I have a search bar and a dropdown selection that lets the user choose which operating system they want to search the problem. However, it is not filtering the operating system that the user chooses. I can't figure out what my code is missing.

 $connect = mysqli_connect("localhost", " ", " ", "os_solutions");  
 if(isset($_POST["submit"]))  
 {  
      if(!empty($_POST["search"]))  
      {  
           $query = str_replace(" ", "+", $_POST["search"]);  
           header("location:search.php?search=" . $query);  
      }  
 }  
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Search Solutions Database</title>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
      </head>  
      <body>  
           <br /><br />  
           <div class="container" style="width:1000px;">  
                <h3 align="center">Search Solutions Database</h3><br /> 
                <form method="post">  
                     <label>Enter Search Text</label>  
                     <input type="text" name="search" class="form-control" value="<?php if(isset($_GET["search"])) echo $_GET["search"]; ?>" />  
                     <br />  
                     <select name="os" value="<?php if(isset($_GET["os"])) echo $_GET["os"]; ?>">
                        <option value="category" style="font-weight:bold;">Select Operting System</option>
                        <option value="windows">Windows</option>
                        <option value="linux">Linux</option>
                        <option value="macos">macOS</option>
                    </select>
                    <br />
                    <br />
                     <input type="submit" name="submit" class="btn btn-info" value="Search" />
                     <br />
                </form>  <br />
               <br /><br />  
                <div class="table-responsive">  
                     <table class="table table-bordered">  
                     <th style="text-align:center;">ID</th><th style="text-align:center;">Name</th><th style="text-align:center;">Issue</th><th style="text-align:center;">Solution</th>
                     <?php  
                     if(isset($_GET["search"]))  
                     {  
                          $condition = '';  
                          $query = explode(" ", $_GET["search"]);  
                          foreach($query as $text)  
                          {  
                               $condition .= "issue LIKE '%".mysqli_real_escape_string($connect, $text)."%' OR ";
                          }  
                          $condition = substr($condition, 0, -4);  
                          $sql_query = "SELECT * FROM windows WHERE " . $condition;  
                          $sql_query = "SELECT * FROM linux WHERE " . $condition;  
                          $sql_query = "SELECT * FROM macos WHERE " . $condition;  
                          
                          $result = mysqli_query($connect, $sql_query);  
                          if(mysqli_num_rows($result) > 0)  
                          {  
                               while($row = mysqli_fetch_array($result))  
                               {  
                                    
                                    echo '<tr>';
                                    echo '<td>'.$row["id"]. '</td>';
                                    echo '<td>'.$row["name"]. '</td>';
                                    echo '<td>'.$row["issue"]. '</td>';
                                    echo '<td>'.$row["solution"]. '</td> </tr>';  
                               }  
                          }  
                          else  
                          {  
                               echo '<label>Data not Found</label>';  
                          }  
                     }  
                     ?>  
                     </table>  
                </div>  
           </div>  
      </body>  
 </html>```


source https://stackoverflow.com/questions/73653704/php-dropdown-with-searach-bar-not-working

No comments:

Post a Comment