Create search query with optional fields - Not Working, need suggestions pls
Hi, need help please asap.
I am trying to search/filter records from a DB table and have used the code from the link below -
http://cookbooks.adobe.com/post_Create_search_query_with_optional_fields-16245.html
However when I run the search, the filtered results are not returned, rather I get back all the table records. Also on the view results page I get the following error:
Notice: Undefined variable: query_search in C:\wamp\www\CIWProject\Bugs\ViewSearchedBugs.php on line 91
relating to the following line of code:
$query_search .= ' WHERE ';
Ani ideas/suggestions/solutions greatly appreciated.
The page code in its entirety:
<?php require_once('../Connections/Project1.php'); ?>
<?php
//initialize the session
if (!isset($_SESSION)) {
session_start();
}
// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
$logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
//to fully log out a visitor we need to clear the session varialbles
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);
unset($_SESSION['PrevUrl']);
$logoutGoTo = "../index.html";
if ($logoutGoTo) {
header("Location: $logoutGoTo");
exit;
}
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_Project1, $Project1);
$query_ViewSearchedBugs = "SELECT BugID, ContractName, Username, LogDate, Priority, `Description`, Status FROM tblbugs";
$ViewSearchedBugs = mysql_query($query_ViewSearchedBugs, $Project1) or die(mysql_error());
$row_ViewSearchedBugs = mysql_fetch_assoc($ViewSearchedBugs);
$totalRows_ViewSearchedBugs = mysql_num_rows($ViewSearchedBugs);
mysql_select_db($database_Project1, $Project1);
$expected = array('ContractName' => 'text', 'Username' => 'text', 'Priority' => 'text', 'Status' => 'text');
$query_ViewSearchedBugs = "SELECT BugID, ContractName, Username, LogDate, Priority, `Description`, Status FROM tblbugs";
// Set a flag to indicate whether the query has a WHERE clause
$where = false;
// Loop through the associatiave array of expected search values
foreach ($expected as $var => $type) {
if (isset($_GET[$var])) {
$value = trim(urldecode($_GET[$var]));
if (!empty($value)) {
// Check if the value begins with > or <
// If so, use it as the operator, and extract the value
if ($value[0] == '>' || $value[0] == '<') {
$operator = $value[0];
$value = ltrim(substr($value, 1));
} elseif (strtolower($type) != 'like') {
$operator = '=';
}
// Check if the WHERE clause has been added yet
if ($where) {
$query_search .= ' AND ';
} else {
$query_search .= ' WHERE ';
$where = true;
}
// Build the SQL query using the right operator and data type
$type = strtolower($type);
switch($type) {
case 'like':
$query_search .= "`$var` LIKE " . GetSQLValueString('%' .
$value . '%', "text");
break;
case 'int':
case 'double':
case 'date':
$query_search .= "`$var` $operator " .
GetSQLValueString($value, "$type");
break;
default:
$query_search .= "`$var` = " . GetSQLValueString($value,
"$type");
}
}
}
}
$ViewSearchedBugs = mysql_query($query_ViewSearchedBugs, $Project1) or die(mysql_error());
$row_ViewSearchedBugs = mysql_fetch_assoc($ViewSearchedBugs);
$totalRows_ViewSearchedBugs = mysql_num_rows($ViewSearchedBugs);
?>
<?php
if (!isset($_SESSION)) {
session_start();
}
$MM_authorizedUsers = "Developer,Project Manager";
$MM_donotCheckaccess = "false";
// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
// For security, start by assuming the visitor is NOT authorized.
$isValid = False;
// When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
// Therefore, we know that a user is NOT logged in if that Session variable is blank.
if (!empty($UserName)) {
// Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
// Parse the strings into arrays.
$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}
// Or, you may restrict access to only certain users based on their username.
if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "") && false) {
$isValid = true;
}
}
return $isValid;
}
$MM_restrictGoTo = "../LoginUnsuccessful.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0)
$MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="../Stylesheets/Stylesheet1.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="Wrapper">
<div id="Banner"> <img src="../Images/Green_bug2.JPG" alt="Banner Image" width="100" class="FloatLeft"/>BugMaster</div>
<?php if ($_SESSION['MM_UserGroup'] == 'Project Manager') { ?>
<div id="Header">
<div class="LinkBox"><a href="../LoggedIn.php">PM HOME</a></div>
<div class="LinkBox"><a href="../Contracts/ViewContracts.php">CONTRACTS</a></div>
<div class="LinkBox"><a href="../Users/ViewUsers.php">USERS</a></div>
<div class="LinkBoxActive"><a href="ViewBugs.php">BUGS</a></div>
<div class="LinkBox"><a href="<?php echo $logoutAction ?>">LOGOUT</a></div>
</div>
<div id="Main">
<h2>View Searched Bugs.</h2>
<p>Search criteria: </p>
<p><a href="AddBug.php">Add Bug</a></p>
<?php if ($totalRows_ViewSearchedBugs == 0) { // Show if recordset empty ?>
<p>No matching records found; <a href="FindBugs.php">search again</a>?</p>
<?php } // Show if recordset empty ?>
<?php if ($totalRows_ViewSearchedBugs > 0) { // Show if recordset not empty ?>
<table width="100%" border="1" cellspacing="auto" cellpadding="2">
<tr>
<th scope="col">Bug ID</th>
<th scope="col">Contract Name</th>
<th scope="col">Username</th>
<th scope="col">Date</th>
<th scope="col">Priority</th>
<th scope="col">Description</th>
<th scope="col">Status</th>
<th scope="col">Edit Bug?</th>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_ViewSearchedBugs['BugID']; ?></td>
<td><?php echo $row_ViewSearchedBugs['ContractName']; ?></td>
<td><?php echo $row_ViewSearchedBugs['Username']; ?></td>
<td><?php echo $row_ViewSearchedBugs['LogDate']; ?></td>
<td><?php echo $row_ViewSearchedBugs['Priority']; ?></td>
<td><?php echo $row_ViewSearchedBugs['Description']; ?></td>
<td><?php echo $row_ViewSearchedBugs['Status']; ?></td>
<td><a href="EditBug.php">Edit</a></td>
</tr>
<?php } while ($row_ViewSearchedBugs = mysql_fetch_assoc($ViewSearchedBugs)); ?>
</table>
<?php } // Show if recordset not empty ?>
<p><a href="ViewBugs.php">View All Bugs</a></p>
</div>
<div id="Footer">
<div class="LinkBox"><a href="../LoggedIn.php">PM HOME</a></div>
<div class="LinkBox"><a href="../Contracts/ViewContracts.php">CONTRACTS</a></div>
<div class="LinkBox"><a href="../Users/ViewUsers.php">USERS</a></div>
<div class="LinkBoxActive"><a href="ViewBugs.php">BUGS</a></div>
<div class="LinkBox"><a href="<?php echo $logoutAction ?>">LOGOUT</a></div>
</div>
<div id="Notes">
<br/>
Image 'Green_Bug2'©Mushii@Wikipedia, used under licence <a href="http://creativecommons.org/licenses/by-sa/3.0/deed.en">http://creativecommons.org/licenses/by-sa/3.0/deed.en</a> , site layout and content©Cian O' Regan 2010.
<br/>
</div>
</div>
<?php } else { ?>
<div id="Header">
<div class="LinkBox"><a href="../LoggedIn.php">DEVELOPER HOME</a></div>
<div class="LinkBoxActive"><a href="AddBug.php">ADD BUG</a></div>
<div class="LinkBoxActive"><a href="ViewBugs.php">VIEW BUGS</a></div>
<div class="LinkBoxActive"><a href="FindBugs.php">SEARCH BUGS</a></div>
<div class="LinkBox"><a href="<?php echo $logoutAction ?>">LOGOUT</a><a href="Links.php"></a></div>
</div>
<div id="Main">
<h2>View Searched Bugs.</h2>
<p>Search criteria: </p>
<p><a href="AddBug.php">Add Bug</a></p>
<?php if ($totalRows_ViewSearchedBugs == 0) { // Show if recordset empty ?>
<p>No matching records found; <a href="FindBugs.php">search again</a>?</p>
<?php } // Show if recordset empty ?>
<?php if ($totalRows_ViewSearchedBugs > 0) { // Show if recordset not empty ?>
<table width="100%" border="1" cellspacing="auto" cellpadding="2">
<tr>
<th scope="col">Bug ID</th>
<th scope="col">Contract Name</th>
<th scope="col">Username</th>
<th scope="col">Date</th>
<th scope="col">Priority</th>
<th scope="col">Description</th>
<th scope="col">Status</th>
<th scope="col">Edit Bug?</th>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_ViewSearchedBugs['BugID']; ?></td>
<td><?php echo $row_ViewSearchedBugs['ContractName']; ?></td>
<td><?php echo $row_ViewSearchedBugs['Username']; ?></td>
<td><?php echo $row_ViewSearchedBugs['LogDate']; ?></td>
<td><?php echo $row_ViewSearchedBugs['Priority']; ?></td>
<td><?php echo $row_ViewSearchedBugs['Description']; ?></td>
<td><?php echo $row_ViewSearchedBugs['Status']; ?></td>
<td><a href="EditBug.php">Edit</a></td>
</tr>
<?php } while ($row_ViewSearchedBugs = mysql_fetch_assoc($ViewSearchedBugs)); ?>
</table>
<?php } // Show if recordset not empty ?>
<p><a href="ViewBugs.php">View All Bugs</a></p>
</div>
<div id="Footer">
<div class="LinkBox"><a href="../LoggedIn.php">DEVELOPER HOME</a></div>
<div class="LinkBoxActive"><a href="AddBug.php">ADD BUG</a></div>
<div class="LinkBoxActive"><a href="ViewBugs.php">VIEW BUGS</a></div>
<div class="LinkBoxActive"><a href="FindBugs.php">SEARCH BUGS</a></div>
<div class="LinkBox"><a href="<?php echo $logoutAction ?>">LOGOUT</a></div>
</div>
<?php } ?>
<div id="Notes">
<br/>
Image 'Green_Bug2'©Mushii@Wikipedia, used under licence <a href="http://creativecommons.org/licenses/by-sa/3.0/deed.en">http://creativecommons.org/licenses/by-sa/3.0/deed.en</a> , site layout and content©Cian O' Regan 2010.
<br/>
</div>
</div>
</body>
</html>
<?php
mysql_free_result($ViewSearchedBugs);
?>
