0

my first post here so hope its not too dumb a question. Im including a search box in my nav bar and I want it to search by product title OR description, is this possible ? The code I have so far is as follows which works fine to search by one field but not two

<div class="search_box">
  <form method="post" name="search" id="search" action="../admin/search_box.php">
    <input type="submit" name="submit" value="Find" style="float: right" />
    <div style="overflow: hidden; padding-right: .5em;">
      <input type="text" name="description" style="width:100%;" />
    </div>
  </form>
</div>

and then for the data processing

<?php 
require ('dbconnection.php');  
$title = filter_var($_POST["title"], FILTER_SANITIZE_STRING);
$description = filter_var($_POST["description"], FILTER_SANITIZE_STRING);

if (isset($_POST['submit'])) // waits for user to enter data into the form
{
    $sql_search_lookup = "SELECT * FROM item WHERE item.description LIKE '%$description%'";

    $result=mysqli_query ($link, $sql_search_lookup) or die ('Problem:'.$sql_search_lookup);

    if (mysqli_num_rows($result) > 0) 
    {                 
        while ($row=mysqli_fetch_array($result))            
        {
?>                              
        <div style="float: left; width: 98%; margin: 10px">
        <p>
            <div style="float:left; width: 20%;">
              <a href="/products_detail.php?itemID=<?php print $row["itemID"]?>">
                <img src="../images/products/<?php print $row['img_file_path'];?> " width="150" height="120">
              </a>
            </div>
            <div style="float:left;width: 75%;vertical-align:middle; font-size:14px; padding:1%;">
              <?php echo "Title: ". $row['title']?><br>
              <?php echo "Description: ".$row['description']?>
            </div>
        </p>
        </div>      
    <?php }
    } 
    else 
    {
        echo "We cant find what you're looking for sorry";
    }
}
    ?>

1 Answer 1

1

Your HTML should be:

<div class="search_box">
  <form method="post" name="search" id="search" action="../admin/search_box.php">
    <input type="submit" name="submit" value="Find" style="float: right" />
    <div style="overflow: hidden; padding-right: .5em;">
      <input type="text" name="description" style="width:100%;" />
    </div>
  </form>
</div>

Your mysql query should be something like:

$sql_search_lookup = "SELECT * FROM item WHERE item.description LIKE '%"+$description+"%' or item.title LIKE '%"+$description+"%'";
Sign up to request clarification or add additional context in comments.

2 Comments

thx for that Arin but what Im trying to find out is if its possible to use the 1 input box to search by description or title instead of using 2 input boxes (this is in the nav bar so space is limited). Im only a few months into my web dev course so most likely its a silly question
Updated the code above and query... check if that works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.