Two textboxes input and search button

#1

Hae All! Please help me fix this issue. I was coding two textboxes,one is that a user is able to find a job that is looking for by keyword insert in the textbox then second textbox is that the user is able to find the job that is looking for by location and plus the submit button.What I want when a user fills in the two textboxes and clicks on the submit it’s able to display result.

I have tried it myself but it’s only displays the results of the location textbox.

<?php
$output = NULL;
// 1. Create a database connection.
$dbhost = "127.0.0.1";
$dbuser = "root";
$dbpass = "password123";
$dbname = "jobsearch";

$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

?>
<!DOCTYPE html>
<html>
   <head>
      <title>Search Box</title>
      <link rel="stylesheet" type="text/css" href="css/style.css">
   </head>
   <body>
      <form method="post" action="#">
         <div id="search-form">
            <div class="search-input">
               <label>What</label>
               <p>Job title, Keywords, or Company</p>
               <input type="search" name="search_box1" id="search">
            </div>
            <div class="search-input">
               <label>Where</label>
               <p>City, Province, or Region</p>
               <input type="search" name="search_box2" id="search">
            </div>
            <input type="submit" value="Find Jobs" name="btn-search">
         </div>
      </form>
      <?php
if (isset($_POST['btn-search'])) {
    $strkey1 = $_POST['search_box1'];
    $strkey2 = $_POST['search_box2'];
    $sql     = " SELECT * FROM users WHERE Keywords = '$strykey1' OR Location = '$strykey2'";
    $result  = mysqli_query($connection, $sql);
    
    if (mysqli_num_rows($result) > 0) {
        // output data of each row
        while ($row = mysqli_fetch_assoc($result)) {
            echo $row['Job title'] . "<br />";
            echo $row['Keywords'] . "<br />";
            echo $row['Company'] . "<br />";
            echo $row['Location'] . "<br />";
        }
    } else {
        echo "No results";
    }
}

mysqli_close($connection);
?>
  </body>
</html>
#2

Have a look in these three lines:

$strkey1 = $_POST['search_box1'];
$strkey2 = $_POST['search_box2'];
$sql = "SELECT * FROM users WHERE Keywords = '$strykey1' OR Location = '$strykey2'";

and see if anything jumps out at you. I’m surprised it applies either of your search terms.

Some other suggestions would be:

  • Move your PHP to before the form, so that when you submit the form, it isn’t redrawn prior to showing the results (unless that’s intentional).
  • Look at prepared statements rather than just concatenating the search terms into the query.
  • Consider the effects of the user leaving either of the text boxes empty, particularly when you allow the user to choose “AND” to specify that they want to search based on keyword and location, not keyword or location.
#3

As another suggestion, this only checks for full exact matches. It would be good to allow for some lenience here so that when I search for “Developer” for example it still suggests jobs for “Web Developer” etc.

#4

I note both search inputs have the same id (“search”) which isn’t allowed. The id value must be unique.

My suggestion would be to output the $_POST array and the SQL query and see if you can figure out what’s wrong:

print_r($_POST);
echo $sql;

And if you’re not using prepared statements you must escape any user inputs or you’re asking for trouble.