I am trying to create an advanced search database page with three input fields, three filters (one for each input) and two Boolean operator filters that can connect the three, filtered inputs with AND OR NOT. I have gotten several hints about incorporating the AND OR operations but have not yet haven't attempted the NOT. What I have doesn't work in any event. The Boolean filter is supposed to connect the first input with the second, and the second Boolean filter is supposed to connect the second input with the third input. Here is the form page:
<?php
include 'header.php';
?>
<form action="SearchFunction.php" method="POST">
<br>
/*input one*/
<input type="text" name="Search1" placeholder="Search1">
<br>
/*filter one*/
<select name = "Index1">
<option>Index1</option>
<option value="Title">Title</option>
<option value="Author">Author</option>
<option value="Institution">Institution</option>
<option value="Year1">Year</option>
</select>
<br>
/*operator one*/
<select name="Operator1">
<option>Operator1</option>
<option>AND</option>
<option>OR</option>
<br>
/*input two*/
<input type="text" name="Search2" placeholder="Search2">
/*filter two*/
<select name = "Index2">
<option>Index2</option>
<option value="Title">Title</option>
<option value="Author">Author</option>
<option value="Institution">Institution</option>
<option value="Year1">Year</option>
</select>
/*operator two*/
<select name="Operator2">
<option>Operator2</option>
<option>AND</option>
<option>OR</option>
<br>
/*input three*/
<input type="text" name="Search3" placeholder="Search3">
/*filter three*/
<select name = "Index3">
<option>Index3</option>
<option value="Title">Title</option>
<option value="Author">Author</option>
<option value="Institution">Institution</option>
<option value="Year1">Year</option>
</select>
<br>
<button type="submit" name="Search">Search</button>
</form>
</body>
</html>
Below is the search function. I have attempted to construct an array that appends each input and Boolean operator and then sends it to the database
<?php
include 'header.php';
?>
<div class="item-container">
<?php
$Search1 = trim($_POST['Search1']);
$Search2 = trim($_POST['Search2']);
$Search3 = trim($_POST['Search3']);
/*Verify that something has been entered into the fields*/
if ($Search1 =="" && $Search2 =="" && $Search3==""){
echo "Please enter a query";
exit;
}
/*Query Processor*/
$where_ary = array();
$w = 0;
if ( $Search1 !="") {
$where_ary[$w] = "Title = '$Search1'";
$w++;
}
if( $Search1 !="") {
$where_ary[$w] = "Author = '$Search1'" ;
$w++;
}
if ( $Search1 !="") {
$where_ary[$w] = " Institution = '$Search1'" ;
$w++;
}
if ( $Search1 !="") {
$where_ary[$w] = " Year1 = '$Search1'" ;
$w++;
}
if ( $Search2 !="") {
$where_ary[$w] = " MATCH (Title) AGAINST (' +$Search2 ' in BOOLEAN MODE ) " ;
$w++;
}
if ( $Search2 !="") {
$where_ary[$w] = " MATCH (Author) AGAINST (' +$Search2 ' in BOOLEAN MODE ) " ;
$w++;
}
if ( $Search2 !="") {
$where_ary[$w] = " MATCH (Institution) AGAINST (' +$Search2 ' in BOOLEAN MODE ) " ;
$w++;
}
if ( $Search2 !="") {
$where_ary[$w] = " MATCH (Year1) AGAINST (' +$Search2 ' in BOOLEAN MODE ) " ;
$w++;
}
if ( $Search3 !="") {
$where_ary[$w] = " MATCH (Title) AGAINST (' +$Search3 ' in BOOLEAN MODE ) " ;
$w++;
}
if ( $Search3 !="") {
$where_ary[$w] = " MATCH (Author) AGAINST (' +$Search3 ' in BOOLEAN MODE ) " ;
$w++;
}
if ( $Search3 !="") {
$where_ary[$w] = " MATCH (Institution) AGAINST (' +$Search3 ' in BOOLEAN MODE ) " ;
$w++;
}
if ( $Search3 !="") {
$where_ary[$w] = " MATCH (Year1) AGAINST (' +$Search3 ' in BOOLEAN MODE ) " ;
$w++;
}
/*integrating boolean operators*/
$temp_where_str = [ ] ;
$boolean = explode( " AND ", $temp_where_str);
$num = count ( $boolean ) ;
for ( $w = 0; $w < $num ; $w++) {
$or_pos = strpos( $boolean[$w], " OR ");
if ( $or_pos > 0 ) {
$boolean[$w] = "(" . $boolean[$w] . ")";
}
if ( $w == 0 ) {
$where = " $boolean[$w] ";
} else {
$where .= " AND $boolean[$w] ";
}
}
/*Create Query*/
$sql = "SELECT * FROM Database WHERE $where " ;
/*send query to database*/
$result = mysqli_query( $conn, $sql ) ;
/*retrieve results*/
$queryResult = mysqli_num_rows( $result ) ;
/*display results*/
echo "Total results: ".$queryResult."";
if ($queryResult > 0) {
/* or use while ($row = mysqli_fetch_assoc($result)) {*/
while ($row = mysqli_fetch_array($result)) {
echo "<a href='2TemplateThreeFieldItem.php?title=".$row['Title']."&title=".$row['Title']."'>
<div class='item-box'>
<h3>".$row['Title']."</h3></a>
<b><label>ID: </label></b>".$row['ID']."
<b><label> Author: </label></b>".$row["Last_Name"] . ", " .$row["First_Name"]."
<b><label> Institution: </label></b>".$row['Institution']."
<b><label> Year: </label></b>".$row['Year1']."
</div>";
}
} else{
echo " - There are no results matching query";
}
?>
</div>
If I type nothing it I get the message "Please enter a query." If I type somethign that is in the database I get "Uncaught TypeError: mysqli_num_rows(): Argument #1 ($result) must be of type mysqli_result, bool given in..." Any review of this would be greatly appreciated. Please let me know if there is an online resource that clarifies how the Boolean operators are supposed to work with the inputs in an array, or if any other information about these scripts is needed.