Jump to content

Search with Multiple fields


KurveMedia

Recommended Posts

I have searched the forums for a post that dealt with my question, but couldn't find one.

 

My question is: I have a search form that needs to look for records in a mysql database. what i want is it to look for records using both fields as the criteria and to display. I have it working using one field, but not sure how i go about using both fields. below is the php code that i have at the moment. I'm using dreamweaver for my code.

 

$colname_rsListing = "-1";
if (isset($_GET['category'])) {
  $colname_rsListing = $_GET['category'];
}
mysql_select_db($database_MscottLinx, $MscottLinx);
$query_rsListing = sprintf("SELECT * FROM listings WHERE MainCat = %s", GetSQLValueString($colname_rsListing, "text"));
$rsListing = mysql_query($query_rsListing, $MscottLinx) or die(mysql_error());
$row_rsListing = mysql_fetch_assoc($rsListing);
$totalRows_rsListing = mysql_num_rows($rsListing);

 

if anybody knows of a post here in the forums that could help me out, could you please point me to that post

 

if you need any more info please let me know

 

Mike

Kurve Media

Link to comment
Share on other sites

DarkWater, thanks for the quick reply

 

Would the code below be correct?

 

 

 


$colname_rsListing = "-1";
if (isset($_GET['category'])) {
  $colname_rsListing = $_GET['category'];
  $colname_rsListing2 = $_GET['zip'];

}

mysql_select_db($database_MscottLinx, $MscottLinx);
$query_rsListing = sprintf("SELECT * FROM listings WHERE MainCat = %s", GetSQLValueString($colname_rsListing, "text") AND BusinessZip = %s", GetSQLValueString($colname_rsListing2, "text"));
$rsListing = mysql_query($query_rsListing, $MscottLinx) or die(mysql_error());
$row_rsListing = mysql_fetch_assoc($rsListing);
$totalRows_rsListing = mysql_num_rows($rsListing);

Link to comment
Share on other sites

wow im getting i guess it would be considered an error, never seen this kind before.

 

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Alimentos y Bebidas '' AND BusinessZip = ''85335''' at line 1"

 

 

not sure what that's all about, here is my php code

 


$colname_rsListing = "-1";
if (isset($_GET['category'])) {
  $colname_rsListing = $_GET['category'];
  $colname_rsListing2 = $_GET['zip'];
}

mysql_select_db($database_MscottLinx, $MscottLinx);
$query_rsListing = sprintf("SELECT * FROM listings WHERE MainCat = '%s'  AND BusinessZip = '%s'", GetSQLValueString($colname_rsListing, "text"), GetSQLValueString($colname_rsListing2, "text"));
$rsListing = mysql_query($query_rsListing, $MscottLinx) or die(mysql_error());
$row_rsListing = mysql_fetch_assoc($rsListing);
$totalRows_rsListing = mysql_num_rows($rsListing);


 

Mike

Kurve Media

Link to comment
Share on other sites

Ok, now this is getting aggravating and i can now understand, why people don't like for Dreamweaver to code php. I think my best bet would to, just scrap the php coded by dreamweaver and code it by hand. thing is im not sure were to start....lol sad i know...

Link to comment
Share on other sites

gigas10

 

I'm not sure what was wrong with it. I'm not that great at php, hence the reason i'm using dreamweaver for it. sorry...

 

 

here is my entire php code:


<?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;
}
}

$colname_rsListing = "-1";
if (isset($_GET['category'])) {
  $colname_rsListing = $_GET['category'];
  $colname_rsListing2 = $_GET['zip'];
}

mysql_select_db($database_MscottLinx, $MscottLinx);
$query_rsListing = sprintf("SELECT * FROM listings WHERE MainCat = %s  AND BusinessZip = %s", GetSQLValueString($colname_rsListing, "text"), GetSQLValueString($colname_rsListing2, "text"));
$rsListing = mysql_query($query_rsListing, $MscottLinx) or die(mysql_error());
$row_rsListing = mysql_fetch_assoc($rsListing);
$totalRows_rsListing = mysql_num_rows($rsListing);
?>



Link to comment
Share on other sites

Hey, sorry about not helping you finish yesterday. =P  Is BusinessZip a number?  If so...

 

$query_rsListing = sprintf("SELECT * FROM listings WHERE MainCat = %s  AND BusinessZip = %s", GetSQLValueString($colname_rsListing, "text"), GetSQLValueString($colname_rsListing2, "text"));

 

Change it to:

$query_rsListing = sprintf("SELECT * FROM listings WHERE MainCat = %s  AND BusinessZip = %d", GetSQLValueString($colname_rsListing, "text"), GetSQLValueString($colname_rsListing2, "int"));

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.