Dreamweaver Advanced Recordset Problems

I am trying to create a recordset that uses several form values, including a text search.  I can get it to wrok fine when there is only one form field to use, but as soon as I start adding more form values, I can't get it to work any more.  I can't figure out what is wrong with this code.  I need to add several more form values after I get this working.  I'm using POST on the form and using the URL to keep track of the language (l).



<?php require_once('Connections/profiles.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $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;
}
}
 
$varProfileText_rs_AZ = "-1";
if (isset($_POST['profile_ProfileText'])) {
  $varProfileText_rs_AZ = $_POST['profile_ProfileText'];
}
$varLang_rs_AZ = "English";
if (isset($_GET['l'])) {
  $varLang_rs_AZ = $_GET['l'];
}
$varCat_rs_AZ = "-1";
if (isset($_POST['profile_categories'])) {
  $varCat_rs_AZ = $_POST['profile_categories'];
}
mysql_select_db($database_profiles, $profiles);
$query_rs_AZ = sprintf("SELECT * FROM profiles WHERE profile_States LIKE '%%Arizona&&' AND profile_ProfileText LIKE %s AND profile_categories LIKE %s AND l = %s AND profile_IsLive ='1' AND profile_StudyUSA ='1' ORDER BY profiles.profile_SchoolName", GetSQLValueString("%" . $varProfileText_rs_AZ . "%", "text") , GetSQLValueString("%" . $varCat_rs_AZ . "%", "text") , GetSQLValueString($varLang_rs_AZ, "text"));
$rs_AZ = mysql_query($query_rs_AZ, $profiles) or die(mysql_error());
$row_rs_AZ = mysql_fetch_assoc($rs_AZ);
$totalRows_rs_AZ = mysql_num_rows($rs_AZ);
 
 
 
 
?>

Open in new window

jwells09Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

v2MediaCommented:
This is because there are default values, such as "-1" for $varCat_rs_AZ, that are applied to the select statement regardless of whether the form input was used or not. Basically a logic error.

Instead of default values, you need to construct the where clause based on what inputs are used, not default values. For e.g. (code untested, reply if there's syntax errors)
<?php require_once('Connections/profiles.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $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;
}
}
 
//where clause items that are always included
$where = "WHERE profile_States LIKE '%Arizona%' "; 
$where .= "AND profile_IsLive = '1' ";
$where .= "AND profile_StudyUSA = '1' ";
 
//where clause items that are conditionally included
$varProfileText_rs_AZ = "-1";
if (!empty($_POST['profile_ProfileText'])) {
  $varProfileText_rs_AZ = GetSQLValueString("%" . $_POST['profile_ProfileText'] . "%", "text");
  $where .= "AND profile_ProfileText LIKE $varProfileText_rs_AZ ";
}
$varLang_rs_AZ = "English";
if (!empty($_GET['l'])) {
  $varLang_rs_AZ = GetSQLValueString($_GET['l'], "text");
  $where .= "AND l = $varLang_rs_AZ "
}
$varCat_rs_AZ = "-1";
if (!empty($_POST['profile_categories'])) {
  $varCat_rs_AZ = GetSQLValueString("%" . $_POST['profile_categories'] . "%", "text");
  $where .= "AND profile_categories LIKE $varCat_rs_AZ ";
}
mysql_select_db($database_profiles, $profiles);
$query_rs_AZ = "SELECT * FROM profiles $where
				ORDER BY profiles.profile_SchoolName";
$rs_AZ = mysql_query($query_rs_AZ, $profiles) or die(mysql_error());
$row_rs_AZ = mysql_fetch_assoc($rs_AZ);
$totalRows_rs_AZ = mysql_num_rows($rs_AZ);
 
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Dreamweaver

From novice to tech pro — start learning today.