This is a very complicated problem. I have an existing web page with multiple drop-down fields-- now I want to add a keyword search text field. I have managed to create a page with the text input only, and that works fine. The trouble comes when I try to add it to the other fields.
The live page is here (sorry, the development page is behind a login):
http://www.technologistsinc.com/Projects/
The original page was developed through DreamWeaver using the Recordset function. Once I learned a bit more about databases, I upgraded it to mysqli. Here is the simplified code:
//database connection
<?php require_once('../Connections/projectConn.php'); ?>
//Main database query
$query_projTbl = "SELECT *, DATE_FORMAT(project_descriptions.proj_date_final, '%M %Y') AS us_format FROM project_descriptions
JOIN ...";
//Bunch of irrelevant JOIN commands to other tables
//Set variable for WHERE clause
$where = false;
//Basic loop that all dropdowns go through. Imagine repeating
$loc='project_descriptions.locat_id';
//If the location field contains a variable
if(isset($_GET['loc'])) {
$loc = $_GET['loc'];
$query_projTbl .= " WHERE project_locations.locat_id=$loc";
$where = true;
}
$client='project_owner.proj_owner_id';
//If the client field contains a variable
if(isset($_GET['client'])) {
$client = $_GET['client'];
if ($where) {
$query_projTbl .= " AND ";
} else {
$query_projTbl .= " WHERE ";
$where = true;
}
$query_projTbl .= "project_owner.proj_owner_id=$client";
}
// ... Here is the Text field
if(isset($_GET['txt_search'])) {
$txt_search=$_GET['txt_search'];
if ($where) {
$query_projTbl .= " AND ";
} else {
$query_projTbl .= " WHERE ";
$where = true;
}
$query_projTbl .= "MATCH (project_descriptions.proj_desc) AGAINST ('%$txt_search%' IN BOOLEAN MODE)";
}
//Tidy up
$query_projTbl .= " ORDER BY proj_date_final DESC";
$projTbl = mysqli_query($projectConn, $query_projTbl) or die(mysqli_error($projectConn));
$row_projTbl = mysqli_fetch_assoc($projTbl);
$totalRows_projTbl = mysqli_num_rows($projTbl);
The text input is named "txt_search" of course.
Basically, two kinds of weird things happen:
The fields don't work together. I get a result only if I enter something into the text field.
I changed "AND" in the text field if... statement to "OR", and then the drop-downs worked only, or the text field worked only, but not both. Changing all of the if... statements to "OR" just made it send all items for every search.
I cannot even think of what track to go in--any suggestions would be heavily appreciated (not to mention any advice on writing this whole application better.)