简体   繁体   中英

Search multiple Criteria - And/or search in PHP

Not sure what I am doing wrong here. I would like to create a search that allows the user to do an and or search.

However when I use the below code, if I type in Brown as Colour1 it will return all results, same as post code.

The goal is to allow the user to search multiple fields to return a match. So Colour1 and Postcode

 <html> <head> <title> Logo Search</title> <style type="text/css"> table { background-color: #FCF; } th { width: 250px; text-align: left; } </style> </head> <body> <h1> National Logo Search</h1> <form method="post" action="singlesearch2.php"> <input type="hidden" name="submitted" value="true"/> <label>Colour 1: <input type="text" name="criteria" /></label> <label>Colour 2: <input type="text" name="criteria2" /></label> <label>PostCode: <input type="text" name="criteria3" /></label> <label>Suburb: <input type="text" name="criteria4" /></label> <input type="submit" /> </form> <?php if (isset($_POST['submitted'])) { // connect to the database include('connect.php'); //echo "connected " ; $criteria = $_POST['criteria']; $query = "SELECT * FROM `Mainlist` WHERE (`Colour1`like '%$criteria%') or ('Colour2' like '%$criteria2%') or ('PostCode' = '%$criteria3%') or ('Suburb' like '%$criteria4%') LIMIT 0,5"; $result = mysqli_query($dbcon, $query) or die(' but there was an error getting data'); echo "<table>"; echo "<tr> <th>School</th> <th>State</th> <th>Suburb</th> <th>PostCode</th> <th>Logo</th> <th>Uniform</th></tr>"; while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { echo "<tr><td>"; echo $row['School']; echo "</td><td>"; echo $row['State']; echo "</td><td>"; echo $row['Suburb']; echo "</td><td>"; echo $row['PostCode']; echo "</td><td><img src=\\"data:image/jpeg;base64,"; echo base64_encode($row['Logo']); echo "\\" /></td></td>"; echo "</td><td><img src=\\"data:image/jpeg;base64,"; echo base64_encode($row['Uniform']); echo "\\" /></td></td>"; } echo "</table>"; }// end of main if statment ?> </body> </html> 

I can get it to work correctly when I use a dropdown list to select the criteria however I would like them to have multiple options to filter results.

 <form method="post" action="multisearch.php"> <input type="hidden" name="submitted" value="true"/> <label>Search Category: <select name="category"> <option value="Colour1">Main Colour</option> <option value="Colour2">Secondary Colour</option> <option value="PostCode">Post Code</option> </select> <label>Search Criteria: <input type="text" name="criteria" /></label> <input type="submit" /> </form> <?php if (isset($_POST['submitted'])) { // connect to the database include('connect.php'); echo "connected " ; $category = $_POST['category']; $criteria = $_POST['criteria']; $query = "SELECT * FROM `Mainlist` WHERE $category LIKE '%$criteria%'"; $result = mysqli_query($dbcon, $query) or die(' but there was an error getting data'); 

In general you run your query with AND condition because it has criteria and you have it means that you have to show value by matching each criteria with receptive column. but it also depends on users or client how they want to show their field.

In short it doesn't have any rule how to show.

Played around with it for a bit and found the answer. I needed to define the criteria. see below code

 <?php if (isset($_POST['submitted'])) { // connect to the database include('connect.php'); //echo "connected " ; $criteria = $_POST['criteria']; $criteria2 = $_POST['criteria2']; $criteria3 = $_POST['criteria3']; $criteria4 = $_POST['criteria4']; $criteria5 = $_POST['criteria5']; $query = "SELECT * FROM `Mainlist` WHERE (`Colour1`like '%$criteria%') and (`Colour2`like '%$criteria2%') and (`PostCode`like '%$criteria3%') and (`Suburb`like '%$criteria4%') and (`State`like '%$criteria5%') 

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